|
Boost Users : |
From: Paul Davis (pjdavis_at_[hidden])
Date: 2006-09-27 12:22:35
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<TestThread*>( data )
tt->run() ;
}
Paul
On 9/27/06, Monica Gretzer <mogr_progr_at_[hidden]> 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 <boost/thread/thread.hpp>
> #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 <boost/thread/thread.hpp>
> #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_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
>
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net