Boost logo

Boost Users :

Subject: [Boost-users] Question about the boost::thread_group
From: watsonsong (911songfeiyu_at_[hidden])
Date: 2010-01-18 22:18:03


I want to use thread_group for multi-thread, and maybe using it like this:

void thread2();
void thread1(boost::thread_group& tg)
{
// create an other thread in the thread_group
tg.create_thread(thread2);
// do some work
for(;;)
{
//...
}

}

void thread2()
{
// do some work
for(;;)
{
//...
}
}

int main()
{
boost::thread_group tg;
tg.create_thread(boost::bind(thread1, boost::ref(tg)));
tg.join_all();
return 0;
}

I am puzzling about the join_all() function. Whether is it safe to invoke
boost::thread_group::create_thread in an other thread and the join_all will
wait for all the tow thread?
I saw the source code of thread_group. It used a std::list to keep thread
objects, and used a shared_mutex for synchronization. It lock on
create_thread and shared lock on join_all.
Is that mean if it run on "tg.join_all()" in main thread before
"tg.create_thread(thread2);" in thread1, the "tg.create_thread(thread2);"
will lead to a dead-lock because the "tg.join_all()" wait for thread1
finish, and thread1 wait for "tg.join_all()" finish on
"tg.create_thread(thread2);"

If it is so, how can I implement such a function?
Thank you.

-- 
View this message in context: http://old.nabble.com/Question-about-the-boost%3A%3Athread_group-tp27220440p27220440.html
Sent from the Boost - Users mailing list archive at Nabble.com.

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