I've written a multithreaded program similar to the following structure (I've omitted the mutex and extraneous code), and it blocks on the call to boost::thread_group.add_thread()
when called from a thread. Is there any way around this, so the call doesn't block?
boost::thread_group group;
void threaded_function2()
{
}
void threaded_function()
{
if( condition)
{
boost::thread *t3 = new boost::thread( boost::bind( &threaded_function2));
group.add_thread( t3); // <-- Blocks on this call
}
}
int main()
{
boost::thread *t1 = new boost::thread( boost::bind( &threaded_function));
boost::thread *t2 = new boost::thread( boost::bind( &threaded_function));
group.add_thread( t1);
group.add_thread( t2);
group.join_all();
return 0;
}
Thanks everyone.
Correct me if I'm wrong, but what might be happening here is that the join_all call ran before the thread getting added, making the thread_group object block until the other threads are released. One solution is to make a mutex on the main function to wait for the threaded_function completes to call the join_all method. This is bad design, though.