
11 Nov
2011
11 Nov
'11
8:14 p.m.
Szymon Gatner wrote:
On the second thought on weak_ptr's rationale with regards of thread-safety, what about this:
if (!weak.expired()) { // here it can expire from other thread weak.lock()->thisWillCauseAssert(); }
Isn't this how weak_ptr<> is suppose to be used?
No, it isn't. if( shared_ptr<> p = weak.lock() ) { p->thisWillNotAssert(); } expired() should only be used for positive tests: if( weak.expired() ). Once a weak_ptr expires, it stays expired, so there is no potential for a race.