Boost logo

Boost :

From: Tomas Puverle (Tomas.Puverle_at_[hidden])
Date: 2004-09-22 10:14:39


John Torjo wrote:

> Hi Peter,
>
> I think there is a bug in your Interlocked* implementation of shared_ptr
> (http://www.pdimov.com/cpp/shared_count_x86_exp2.hpp).
>
> In atomic_read you have:
> inline long atomic_read(long volatile const & value)
> {
> return value;
> }
>
> I don't believe this is thread-safe.
> In order to make sure you're receiving the latest value, I assume you
> could do something like:
>
> inline long atomic_read(long volatile & value)
> {
> const int IMPOSSIBLE_VALUE = -100;
> return InterlockedCompareExchange(&value, IMPOSSIBLE_VALUE,
> IMPOSSIBLE_VALUE);
> }

Here are my two cents:

On other processors, a compiler could make the assumption that unless it
sees an explicit memory barrier (or some code which it cannot analyse)
it can just cache the value of a variable (whether or not declared as
volatile).

On x86 you don't need to assert any memory barriers to ensure visibility
of data, hence the compiler should assume that any data declared as
volatile could potentially change without it knowing so, and hence
should reload any such data each time.

So I think the above should be safe on the x86.

Tom


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