RE: [boost] [shared_ptr] Interlocked* - possible bug?

John Torjo <john.lists@torjo.com> 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 don't believe that's actually important. What's important is that shared memory accesses are not reordered. The "volatile" should prevent the compiler from reordering the read with respect to other volatile access, but that leaves the question of whether the processor may reorder the read with respect to other shared memory access. I think the answer is that it cannot change the order of an atomic_read and an atomic update, assuming that the latter uses an Interlocked* operations which includes a full memory barrier, but unfortunately it would be able to change the order of two atomic_reads. (Unfortunately www.pdimov.com does not want to talk to me so I haven't been able to read the rest of the code.)
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); }
Am I missing something?
That looks reasonable, yet I feel that there should be a better way to do this! Note that InterlockedCompareExchange is not available in early Win32 implementations (NT 3.x and Win95).

Ben Hutchings wrote:
(Unfortunately www.pdimov.com does not want to talk to me so I haven't been able to read the rest of the code.)
(repost) Yes, pdimov.com is down, as is mmltd.net - my mail server, which explains my inactivity.

Ben Hutchings wrote:
I think the answer is that it cannot change the order of an atomic_read and an atomic update, assuming that the latter uses an Interlocked* operations which includes a full memory barrier, but unfortunately it would be able to change the order of two atomic_reads.
It doesn't matter whether two reads are reordered or not, because they will see the same value regardless of the order.
participants (2)
-
Ben Hutchings
-
Peter Dimov