Boost logo

Boost Users :

Subject: [Boost-users] weak_ptr and thread safety
From: Pavel Shevaev (pacha.shevaev_at_[hidden])
Date: 2009-07-22 15:05:46


Guys, could you please clarify whether the following usage of weak_ptr
is thread safe:

//thread A:
shared_ptr<Foo> p = wp.lock();

//thread B:
p.reset();

I believe it's not thread safe as it looks very similar to the example
3 from the shared_ptr documentation:

// thread A
p = p3; // reads p3, writes p

// thread B
p3.reset(); // writes p3; undefined, simultaneous read/write

Am I right in my assumption?

And if yes, could you please give any recommendations for the
following use case.

I have a networking thread which owns a set of net session objects
stored as shared_ptrs. I also have another thread where some entities
have weak_ptrs members pointing to net session objects from networking
thread. From time to time net sessions are erased and I think when at
the very this moment entity object from another thread tries to lock()
the net session and access it seg. fault happens.

Thanks in advance.

-- 
Best regards, Pavel

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net