
You need a global wrapper function.
Ie, a thread start function that takes the address of the instance of the
class as the void* pointer and then runs some method in the class.
(Generally a run method or something similar)
something like:
void* start_thread( void* data )
{
TestThread* tt = dynamic_cast
Hi,
I want to use boost::thread to create threads that are member functions within a class. In all the examples I have seen, the thread function is defined globally.
I explain it with code. The following test code works:
// code 1: example program that works ----------------------------------------------- #include
#include <iostream> using namespace std; void testing() { cout << "I'm a thread." << endl; }
int main(int argc, char* argv[]) { // start a new thread that calls the "testing" function boost::thread my_thread(&testing); // wait for the thread to finish my_thread.join();
return 0; }
// -------------------------------------------------------------------------------------------
Now, I want to create something like:
// code 2: thread created from member class (code yields compiler errors)
#include
#include <iostream> using namespace std; class TestThread { public: void testing(void); void CreateThread(void); };
void TestThread::CreateThread() { // start a new thread that calls the "testing" function boost::thread my_thread(&testing); // wait for the thread to finish my_thread.join(); }
int main(int argc, char* argv[]) { TestThread t; t.CreateThread();
return 0; }
When compiling the code above (code 2), I get the following compilation error: ISO C++ forbids taking the address of an unqualified or paranthesized non-static member function to form a pointer to member function. Say &TestThread::testing.
My question is: How do I create threads from within a C++ class member function?
Thanks a lot for your help.
/ Mogr
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users