boost thread to run a method of my object (full source

class Worker{
    Worker()    {
        // the thread is not-a-thread ie a dummy thread object, until we assign a real thread object
    void start(int N)    {
        // pass “this” as first arg to processQueue(), which runs not as a method but as a free func in its own thread
        m_Thread = boost::thread(&Worker::processQueue, this, N);
    void join(){m_Thread.join();}
    void processQueue(unsigned N)    {
        float ms = N * 1e3;
        boost::posix_time::milliseconds workTime(ms);
        std::cout << "Worker: started, will work for "
                  << ms << "ms"
                  << std::endl;
        std::cout << "Worker: completed" << std::endl;
    boost::thread m_Thread;
int main(int argc, char* argv[]){
    std::cout << "main: startup" << std::endl;
    Worker worker;
    std::cout << "main: waiting for thread" << std::endl;
    std::cout << "main: done" << std::endl;
    return 0;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s