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