boost::thread - Why does this code not work?

Hi all,
I have some problems with the boost::thread library, I've been staring at
this code for a couple of days now but still have no idea what's wrong
with it. The code starts a thread, the thread waits at a barrier, and
command is transfered between the main thread and the new thread a couple
of times (using two successive calls to barrier->wait() for each
transition). This works fine most of the times, but sometimes the thread
for some reason does not start. And when the main thread then waits at the
barrier, the program obiously locks up.
Here's the code:
#include <iostream>
#include

"Henrik Berg"
I have some problems with the boost::thread library, I've been staring at this code for a couple of days now but still have no idea what's wrong with it. The code starts a thread, the thread waits at a barrier, and command is transfered between the main thread and the new thread a couple of times (using two successive calls to barrier->wait() for each transition). This works fine most of the times, but sometimes the thread for some reason does not start. And when the main thread then waits at the barrier, the program obiously locks up.
First off, this seems a bizarre use of a barrier. Why not just use a mutex? Secondly, the thread is running a *copy* of ThreadObject. Since you're using a pointer to the barrier, this is OK, as the pointer will be copied and the copy will therefore access the same barrier object, but it might cause problems when you get as far as deleting the dynamically-allocated barrier and thread objects. Next, you are not detaching or joining with your threads, so you are spawning more and more threads. Eventually you will run out of threads. I can't think why this would cause your code to break though. Most bizarre. Anthony -- Author of C++ Concurrency in Action | http://www.manning.com/williams just::thread C++0x thread library | http://www.stdthread.co.uk Just Software Solutions Ltd | http://www.justsoftwaresolutions.co.uk 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
participants (2)
-
Anthony Williams
-
Henrik Berg