Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-10-02 08:51:34


Monica Gretzer wrote:
> Hi,
>
> Thanks for your answers. Now I wonder how to a certain number of
> threads which is decided at runtime and can vary between program
> runs. I send the number of threads as an argument to CreateThreads.

[...]

> void TestThread::CreateThreads(int nb)
> {
> boost::thread t1( boost::bind( &TestThread::thread, this, 1));
> boost::thread t2( boost::bind( &TestThread::thread, this, 2));
> boost::thread t3( boost::bind( &TestThread::threas, this, 3));
>
> t1.join();
> t2.join();
> t3.join();
> }

If you really like to wait for the threads to finish (which is what you do
with tN.join), you need to use a thread_group:

http://www.boost.org/doc/html/thread_group.html

void TestThread::CreateThreads(int nb)
{
    boost::thread_group tg;

    for( int i = 0; i < nb; ++i )
    {
        tg.create_thread( boost::bind( &TestThread::thread, this, i ) );
    }

    tg.join_all();
}


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