Boost logo

Boost Users :

From: Ben Hutchings (ben.hutchings_at_[hidden])
Date: 2005-02-11 07:08:02


Paul wrote:
>
>> Synchronous cancellation may be simulated by setting a simple flag
>> polled by the cancellable thread. (Appropriate memory barriers must
>> be placed after setting and before polling the flag. In the absence
>> of portable memory barrier facilities, one may protect the flag with a
>> mutex, though that's annoyingly heavyweight.) However this does not
>
>
> just out of curiousity, why do you think you need a memory barrier on a
> flag? you would have 1 writer and 1 reader... either the reader reads
> false or true, and once the writer has set it to true... thats it, the
> reader will read true on the next pass.

In practice, if you use "volatile" then the compiler will probably
generate code to read the flag from memory every time and the reader
will probably see the change, possibly after some small delay for it to
work its way through the write queue on the processor that the writer is
running on. AFAIK no standard or specification guarantees this.

> even if the reader is reading the same time as the writer is writing,
> and byte-sized memory writes are not atomic (which i think they are),

If they aren't then mutexes won't actually help much. Consider for
example char a[2] with access to a[0] and a[1] controlled by different
mutexes.

> then the read will still evaluate to true, which is what is desired anyway.
>
> so theres no need for a mutex in this case? even if there were
> multiple writers, its a one-way flag set operation, all would be trying
> to set the flag to true if all were writing at the same time, so all
> would get the desired result?

In practice I think that's true, but what do you think guarantees that?
  If there's no guarantee then it might stop working in future.

Ben.


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