Boost logo

Boost :

From: John Torjo (john.lists_at_[hidden])
Date: 2004-09-25 06:40:32

Alexander Terekhov wrote:

> John Torjo wrote:
> [...]
>>Whenever a thread (other than main) wants to access this window, it will
>>query the weak_pointer. The weak_pointer needs to know the LATEST
>>reference count in order to know if the weak pointer is still valid.
>>Thus, atomic_read that simply returns the value is not enough.
> No barriers needed for its lock() call (and shared_ptr-from-weak_ptr
> ctor). It can't see 'false' zero unless you violate the "basic" thread-
> safety contract. Value consistency is ensured by conditional increment

which is?

> using 'naked' (w/o any barriers) CAS (LL/LR-SC aside for a moment) IFF
> the observed 'old' value is not zero.

(after about 3 hours of reading different threads on google)

I still think I'm right ;)

Here's my scenario:
(remember - we're talking about:

class test { ... };

shared_ptr<test> p1( new test); // refcount = 1

---------------- thread switch
shared_ptr<test> p2(p1); // refcount = 2
weak_ptr<test> weak(p2);
p2 = shared_ptr<test>(); // refcount = 1

------------------thread switch
p1 = shared_ptr<test>(); // refcount = 0 - object gets destroyed
---------------- thread switch

// here - you need InterlockedCompareExchange
// to realize that refcount is 0 (zero)
shared_ptr<test> p3 = weak.lock();

Also, see my other post to Peter Dimov.


John Torjo
-- john_at_[hidden]
Contributing editor, C/C++ Users Journal
-- "Win32 GUI Generics" -- generics & GUI do mix, after all
-- v1.4 - save_dlg - true binding of your data to UI controls!
    + easily add validation rules (win32gui/examples/smart_dlg)

Boost list run by bdawes at, gregod at, cpdaniel at, john at