Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use C++ member function in thread pool #7

Closed
rajenk opened this issue Feb 15, 2018 · 5 comments
Closed

How to use C++ member function in thread pool #7

rajenk opened this issue Feb 15, 2018 · 5 comments
Assignees
Labels

Comments

@rajenk
Copy link

rajenk commented Feb 15, 2018

First of all thanks for your detailed write up to explain the code!

I am trying to use the threadpool submit function to take a C++ class member function. I am not able to compile. I followed standard std::thread submitting a class member function method. Like the code shown below.

class A
{
   int i;
public:
      A() { i =10; };
    ~A()
    void func1() { i++;};
};

A objA;

ThreadPool tPool(10);

tPool.submit(&A::func1,&objA);

But I am getting the following error.
error: no matching function for call to ‘ThreadPool::submit(void (A::)(), A)’

Does the current submit() template function accept class member function? If not can you provide some tip as to how this function can be modified to accept class member function?

Appreciate your help.

Here is the std::thread method of passing a non static member function.

class Task
{
public:
	void execute(std::string command);
};

Task * taskPtr = new Task();

// Create a thread using member function
std::thread th(&Task::execute, taskPtr, "Sample Task");

Thanks,
Raj

@mtrebi mtrebi self-assigned this Feb 15, 2018
@mtrebi
Copy link
Owner

mtrebi commented Feb 15, 2018

Hey! I am glad you liked the readme, it took me some time to write it 👍

Regarding your question, yes, there's a way to pass member functions. Your approach doesn't work because the submit function is implemented in such way that the first argument is always the function and the following arguments are the parameters of the function. Probably I could override the submit function or something similar to make it easier...In the meantime, you can solve the issue using std::bind. The idea is basically to create a new function that is simply the function we want to call with the specific object from which we want to call it.

auto func = std::bind(&A::func_1, &objA);
pool.submit(func);

You could do exactly the same using lambda expressions,

pool.submit([&]() {objA.func1(); });

Hope this helps!

@rajenk
Copy link
Author

rajenk commented Feb 15, 2018

Thanks. I will give std::bind a try or use lambda expressions. I will post the results to help answer others visiting your project.

Appreciate your quick reply.

@mtrebi
Copy link
Owner

mtrebi commented Feb 15, 2018

Whenever I have some more time I will try to add an easier way to do it. I am really busy lately...

@rajenk
Copy link
Author

rajenk commented Mar 4, 2018

I tried both bind and lambda approach. Both did not work the way I wanted in my app! So using a C wrapper functions to invoke member functions via the class object. This works for me as I intended.

@mtrebi
Copy link
Owner

mtrebi commented Mar 4, 2018

I tried both options and they worked for me. What do you mean with ¨Both did not work the way I wanted?

Anyway, I am glad you found the solution to your problem 👍

@mtrebi mtrebi closed this as completed Jun 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants