Boost logo

Boost Users :

Subject: Re: [Boost-users] weak_ptr and thread safety
From: Frank Mori Hess (frank.hess_at_[hidden])
Date: 2009-07-22 15:24:24


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wednesday 22 July 2009, Pavel Shevaev wrote:
> 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();

It's safe, presuming your "p" in thread A is a different shared_ptr object
from the "p" reset in thread B. They can safely be copies sharing ownership
of the same object.

> 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

In this unsafe case, thread A and B are manipulating the same shared_ptr
object.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkpnZ2gACgkQ5vihyNWuA4VUPACfXSYjZHiK9dkNnjAziUHFK5S9
KAAAnRYrm4HRILt05OBE/9iw2NaKJSqa
=nfKj
-----END PGP SIGNATURE-----


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