create threads from within a class

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

just use boost::bind, like this
boost::thread my_thread( boost::bind( &testing, this ) );
Also, you haven't defined the member function testing.
Christian
On 9/27/06, Monica Gretzer
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

Actually the correct code would be:
boost::thread t( boost::bind( &TestThread::testing, this ) );
On 9/27/06, Christian Henning
just use boost::bind, like this
boost::thread my_thread( boost::bind( &testing, this ) );
Also, you haven't defined the member function testing.
Christian
On 9/27/06, Monica Gretzer
wrote: 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
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

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

Monica Gretzer wrote:
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.
This indeed should be a FAQ. class foo { public: foo() {} ~foo() {} // the following is what can be seen as kind of a "run" member void operator()(void) { std::cout << "Hello Thread!" << std::endl; } } Somewher you instantiate your class: foo my_foo; Then run it: boost::thread th(my_foo); Later join it: t.join(); You might also read about the boost::bind and functional, as the argument to the thread constructor is accepting a functional. Regards, Roland
participants (5)
-
Christian Henning
-
Johan Alkemade
-
Monica Gretzer
-
Paul Davis
-
Roland Schwarz