Boost logo

Boost :

From: Tom Becker (voidampersand_at_[hidden])
Date: 2002-01-20 16:39:07


On Sun, 20 Jan 2002 08:45:41 -0500, "David Abrahams"
<david.abrahams_at_[hidden]> wrote:
>----- Original Message -----
>From: "Tom Becker" <voidampersand_at_[hidden]>
>
> > In order to support counting safely, it will need a mutex to protect
> > the count.
>
>I don't see why. Only one thread should be touching that count. A disabler
>in a different thread uses a different count.

Okay, I was starting with the assumption that the disabler could
operate across threads. It's good to question that assumption, and it
made me think of something else.

If a disabler is embedded in an object which is allocated on the heap
by one thread, and later deleted by another thread, it could
decrement a different cancellation count than the one that it
incremented. It would be hard to write code to prevent this. It can
easily fixed to do the right thing by giving the disabler a reference
to its originating thread, but then you really do need atomicity on
the count.

When I woke up this morning, I was worried that even more
synchronization might be needed, but then I figured out that it's
okay. Taking action on a cancellation can happen only when the victim
thread hits a cancellation point. There's no way that the creation or
destruction of a disabler is going to happen inside a victim thread
at the same time as a cancellation point, because a single thread
can't do two things at the same time. There could be a problem if all
the victim thread's disablers are on the heap, and another thread
decides to delete them all at just the wrong time, but that's so
perverse it's not realistic.

   Tom

-- 
Tom Becker                      "Within C++, there is a much smaller and
<voidampersand_at_[hidden]>        cleaner language struggling to get out."
                                                       -- Bjarne Stroustrup

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk