
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(); }