General Category > General Discussion about just::thread
creating a thread using a class member function
(1/1)
kkerbel:
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();
Anthony Williams:
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:
--- Code: ---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
--- End code ---
You might like to see my Multithreading in C++0x tutorial series. Member functions are covered in Part 3: Starting Threads with Member Functions and Reference Arguments
kkerbel:
As always...thanks! :)
Navigation
[0] Message Index
Go to full version