Boost logo

Boost Users :

Subject: [Boost-users] boost::weak_ptr usage in multithreaded environments
From: Virgil Palanciuc (virgilp_at_[hidden])
Date: 2010-02-01 09:54:47


Hello all,

[long story short: I'd like to know whether a weak_ptr.lock() counts
as a "shared pointer read" or as a "shared pointer write" ]

I'm using boost shared pointers in an multi-threaded app. Basically,
my app maintains a "forest" of "context trees" - i.e. I have a map
from a "file name" to a "global context" that organizes the
information for that file.
The "global context" in the map is basically the root node of a tree
of "local contexts" in that file. The tree is kept with
boost:shared_ptr<> on the way down (father-son relations) and
boost::weak_ptr<> on the way up (son->father pointers).

I have added proper synchronization to the data structure... I think.
I use reader/writer locks (only one writer allowed, but arbitrary
number of readers) and I'm still getting sporadic crashes, especially
when constructing a "global context" (root node) shared_ptr<> from a
weak_ptr<> - using lock().
I am assuming in my app. that weak_ptr<>.lock() only requires a "read
lock", not a "write lock".. is this true?
I've checked out the page at
http://www.boost.org/doc/libs/1_41_0/libs/smart_ptr/shared_ptr.htm#ThreadSafety,
but there's nothing about weak_ptr there...

Thanks,
   Virgil.


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