Boost logo

Boost Users :

From: Johan Alkemade (johanalkemade_at_[hidden])
Date: 2006-09-28 03:02:46


Actually the correct code would be:

boost::thread t( boost::bind( &TestThread::testing, this ) );

On 9/27/06, Christian Henning <chhenning_at_[hidden]> wrote:
>
> 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 <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 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