Given object x, I'd like to launch a new thread to invoke x.a()
or even x.a(1,2,3)
. I can use boost::thread
to do this for a non-member function; but how do I do this for a member function? How do I pass the this
pointer?
In general, there's lots of STL and Boost code and templates which take code as a parameter, but, as this is not really defined in C++ (functions aren't first class vals, no native lambda support), I'm very confused how they're defined. I can do trial and error but I'd like something cleaerer and more reliable.
UPDATE: My question concerns primarily passing a method; when I try to do what would be the obvious syntax (ClassName::method_name, instance)
I get invalid use of non-static member function
error: no matching function to call
. It would help if you could show the proper syntax to use Boost.thread on a non-static method.
UPDATE 2: I found why I'm struggling with this. The answers given, such as by @OrDinari, work great for boost::thread
. But, when I try using a thread_group
, that is, thread_group::create_thread
, I get this error:
error: no matching function for call to ‘boost::thread_group::create_thread(void (C::*)(), C*)’
So:
- Why does it work for single threads but not thread groups?
- How do I use create a thread in a group to run a member function?