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
Comments
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! |
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. |
Whenever I have some more time I will try to add an easier way to do it. I am really busy lately... |
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. |
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 👍 |
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.
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.
Thanks,
Raj
The text was updated successfully, but these errors were encountered: