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:
http://www.pdimov.com/cpp/shared_count_x86_exp2.hpp)

class test { ... };

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

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

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

// 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.

Best,
John

-- 
John Torjo
-- john_at_[hidden]
Contributing editor, C/C++ Users Journal
-- "Win32 GUI Generics" -- generics & GUI do mix, after all
-- http://www.torjo.com/win32gui/
-- 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 acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk