weak_ptr::lock is not thread safe

Hi All, I've just realized weak_ptr::lock is not thread safe. Is it expected behavior? Following sample reproduced this conclusion. #include <iostream> #include <boost/shared_ptr.hpp> #include <boost/thread.hpp> #include <boost/weak_ptr.hpp> #include <boost/interprocess/detail/atomic.hpp> #include <boost/interprocess/sync/interprocess_semaphore.hpp> static volatile int v = 0; boost::interprocess::interprocess_semaphore s1(0); boost::interprocess::interprocess_semaphore s2(0); boost::interprocess::interprocess_semaphore s3(0); class SomeClass{ public: boost::shared_ptr<SomeClass> s_this_; boost::weak_ptr<SomeClass> w_this_; SomeClass() { s_this_.reset( this ); w_this_ = s_this_; v = 1; } void release() { s_this_.reset(); } ~SomeClass() { v = 0; } }; SomeClass *c; void worker() { int counter = 0; for( ;; ) { s1.wait(); // wait for created object boost::weak_ptr<SomeClass> w = c->w_this_; // make weak ptr s2.post(); // let main thread go ahead boost::shared_ptr<SomeClass> s = w.lock(); // create shared_ptr from weak_ptr if( s ) // test for valid shared_ptr { if( 0 == v ) // if 0, object is destroyed { std::cout << "!!!!!!!! : " << counter << std::endl; exit(1); } } s3.post(); // notify main thread about test completion ++counter; } } int main() { boost::thread t = boost::thread( &worker ); for( ;; ) { c = new SomeClass(); // create object s1.post(); // notify thread about new object s2.wait(); // wait for thread to make weak_ptr c->release(); // destroy object s3.wait(); // wait for thread complete } return 0; } Best regards. Dima -- Dmytro Ovdiienko

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday 16 December 2010, Dmytro Ovdiienko wrote:
I've just realized weak_ptr::lock is not thread safe. Is it expected behavior? Following sample reproduced this conclusion.
It is thread safe. Your code appears to have a race in it between destroying the shared_ptr and locking the weak_ptr. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAk0KPcEACgkQ5vihyNWuA4WVkACeIk8QpbEhF5/k5szY2o6rJ7Ep gHgAoOVPbV/uBAsZTWWjqrwClK6ITZeK =6DeE -----END PGP SIGNATURE-----

Frank, Thanks. My program sometimes generates segfault. Looks like problem is not in the boost :-\ On 16 December 2010 18:26, Frank Mori Hess <frank.hess@nist.gov> wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On Thursday 16 December 2010, Dmytro Ovdiienko wrote:
I've just realized weak_ptr::lock is not thread safe. Is it expected behavior? Following sample reproduced this conclusion.
It is thread safe. Your code appears to have a race in it between destroying the shared_ptr and locking the weak_ptr.
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux)
iEYEARECAAYFAk0KPcEACgkQ5vihyNWuA4WVkACeIk8QpbEhF5/k5szY2o6rJ7Ep gHgAoOVPbV/uBAsZTWWjqrwClK6ITZeK =6DeE -----END PGP SIGNATURE----- _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Dmytro Ovdiienko e-mail: dmitriy.ovdienko@gmail.com skype: dmitriy.ovdienko@gmail.com mobile: +38050-1909731

From: Dmytro Ovdiienko:
class SomeClass{ public: boost::shared_ptr<SomeClass> s_this_; boost::weak_ptr<SomeClass> w_this_;
SomeClass() { s_this_.reset( this ); w_this_ = s_this_;
Shouldn't you be using shared_from_this()? Creating a shared_ptr from "this" doesn't work otherwise. Check out the shared_ptr docs for more info. Cliff
participants (3)
-
Cliff Green
-
Dmytro Ovdiienko
-
Frank Mori Hess