-1

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.

nickb
  • 59,313
  • 13
  • 108
  • 143

1 Answers1

3

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.

scooterman
  • 1,336
  • 2
  • 17
  • 36
  • This is what was happening and I realized that it must be a bad design if it doesn't work, so I've refactored my design to a much more fluid structure that still satisfies my needs. Thanks! – nickb Oct 23 '11 at 03:47