just::thread Support Forum
General Category => General Discussion about just::thread => Topic started by: kkerbel on July 30, 2012, 04:51:48 AM
-
class a {
void afunction(int a);
...
};
//this is obviously an incomplete class...just trying to make clear what I'm trying to do... :-)
how do I go about doing something like...
thread dosomething(afunction, 32);
dosomething.detach();
-
To create a thread using a class member function you need to pass a "this" pointer as the second parameter to the std::thread constructor:
a an_a_object;
std::thread dosomething(&a::afunction, &an_a_object,32); // a pointer to an a
std::thread t2(&a::function,std::ref(an_a_object),32); // a reference wrapped in std::ref
std::thread t3(&a::function,a(),32); // an rvalue, which is copied/moved into the thread storage
You might like to see my Multithreading in C++0x (http://www.justsoftwaresolutions.co.uk/threading/multithreading-in-c++0x-part-1-starting-threads.html) tutorial series. Member functions are covered in Part 3: Starting Threads with Member Functions and Reference Arguments (http://www.justsoftwaresolutions.co.uk/threading/multithreading-in-c++0x-part-3.html)
-
As always...thanks! :)