Boost 1.33 Thread Creation with arguments

Instead of using boost:thread(<Functionname>, <arg1>, … <argn>) we have to use boost:thread ThreadName(boost::bind(<Functionname>,arg1,…,argn))

Example:

void workerFunc(int i)
{
    boost::xtime t;
    boost::xtime_get(&t,boost::TIME_UTC);
    t.sec += i;

    std::cout << "Worker: running, waiting "<< i << " seconds…" << std::endl;

    // Pretend to do something useful…
    boost::thread::sleep(t);

    std::cout << "Worker: wtime="<< i << " finished" << std::endl;
}

int main(int argc, char* argv[])
{
    std::cout << "main: startup" << std::endl;

    boost::thread workerThread(boost::bind(workerFunc,3));
    boost::thread workerThread2(boost::bind(workerFunc,5));

    std::cout << "main: waiting for thread" << std::endl;

    workerThread.join();
    workerThread2.join();

    std::cout << "main: done" << std::endl;

    return 0;
}

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s