
On Mon, May 2, 2011 at 4:23 PM, Ovanes Markarian
On Mon, May 2, 2011 at 3:01 PM, Frank Mori Hess
wrote: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On Monday, May 02, 2011, Peter Dimov wrote:
// thread B // p2 goes out of scope: undefined, the destructor is considered a
"write access"
This example just says that you can't make a copy of a destroyed shared_ptr, or one that is in the process of being destroyed. In principle, it has nothing to do with reference counts. Doing something with a destroyed object of any type is undefined behavior in C++.
It looks like the example isn't quite right though, as p2 was declared in thread A so it can't go out of scope in thread B.
It still can happen if p2 is a weak_ptr in thread "A" and the corresponding shared_ptr goes out of scope in thread "B".
Best Regards, Ovanes
Actually, I am not sure with this statement. Destructor is only called if shared count reaches zero. I don't think that this can happen, that a dtor is called in one thread and the other thread is still able to increment the shared count even from the weak_ptr. I remember reading weak_ptr's source and saw that it is also protected again such a case. Thanks, Ovanes