Boost logo

Boost Users :

From: Ben Hutchings (ben.hutchings_at_[hidden])
Date: 2004-06-11 09:43:14

Tristan King <tristanking_at_[hidden]> wrote:
> Hi,
> I'm having trouble getting shared_ptr objects and threads to work in
> harmony.
> i'm not worried about the order that the threads run in (as this is
> just an example of the problem i'm having in a simple multi-threaded
> web server i'm working on), but it seems like the shared_ptr object
> may be getting deleted before the last thread gets to execute.

Yes, that is what is happening. The problem is that your
single_thread objects are destroyed at the end of the loop body.
By the time the new thread runs its single_thread may have been
destroyed along with all its members. Since each single_thread
is constructed in the same block of memory, it is possible for
the thread to pick up the value intended for the next thread.

> can anyone give me any pointers on how to get around this problem?

I suggest you create your thread objects with 'new' instead, or
else define an array of them outside the loop.


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at