Boost logo

Boost Users :

From: Roland Schwarz (roland.schwarz_at_[hidden])
Date: 2006-09-29 03:31:41


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


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