Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::thread - Why does this code not work?
From: Anthony Williams (anthony.ajw_at_[hidden])
Date: 2009-06-04 11:13:31


"Henrik Berg" <henrikbe_at_[hidden]> writes:

> 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

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