Question about the boost::thread_group

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.
It would really help if you used code tags.

I don't think that the behavior of join_all() has been defined for this. Personally I would recommend managing all thread creation locally. Have the main thread manage its group of threads, have thread1 manage it children, etc. IOW you are creating a hierarchy of threads. You should manage them as such.

One really good rule of thumb with threads is that the only immutable data can be shared across threads. Following that advice would eliminate the sharing of tg with siblings in the way you are doing here.
Topic archived. No new replies allowed.