Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::weak_ptr usage in multithreadedenvironments
From: Peter Dimov (pdimov_at_[hidden])
Date: 2010-02-01 11:15:42


Virgil Palanciuc wrote:

> As I said, I have a global map that keeps the "tree roots"(as
> shared_ptr), and potentially many "walkers" that walk the tree. If two
> threads get the root from the global map - they'd get to read the very
> same shared_ptr variable; If two threads happen to walk the same node
> and try to get the "father" - they'd get to "lock" on the same
> weak_ptr variable simultaneously. Is that a problem?

No, it should work.

> (assuming that I
> have "read locks" in place - i.e. there is currently no "writer"
> thread that is trying to completely remove a tree from the global map)
> [BTW - that's the only way that "writing" ever occurs: a "writer
> thread" builds an alternate version of a tree, and then acquires a
> write lock and replaces the old tree in the global map; there may be
> still walker threads that walk the "old tree" - but that should be ok
> since they keep a copy of the shared_ptr, basically guaranteeing that
> the old tree itself is not destroyed as long as there is a walker on
> it ]

Everything you describe sounds fine, at least in principle. There could be a
bug in weak_ptr, a bug in your logic somewhere, or a bug in Snow Leopard's
gcc (such things have happened in the past). If you suspect weak_ptr you
could try to write a simple test that repeatedly locks the same instance
from several threads and see if it cracks under the pressure.


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