|
Boost Users : |
Subject: Re: [Boost-users] shared_ptr and weak_ptr concurrency
From: Chris Uzdavinis (cuzdav_at_[hidden])
Date: 2009-09-04 12:17:36
On Thu, Sep 3, 2009 at 2:30 AM, Stefan Strasser<strasser_at_[hidden]> wrote:
>> > mutex.lock()
>> > weak_ptr<MyClass> local_weak_ptr (globally_ptr) ;
>> > mutex.lock()
>
> why would you even need a lock here?
Because it's unsafe otherwise.
> the shared_ptr doc says that you can expect the same thread safety from
> shared_ptr as you can from built-in types.
This is true, but you're drawing the wrong conclusion from it.
You need a lock around built-in types as well. You need to use
an "atomic" type to safely do what you're saying.
In the same documentation, they give examples which are
enlightening:
//--- Example 3 ---
// thread A
p = p3; // reads p3, writes p
// thread B
p3.reset(); // writes p3; undefined, simultaneous read/write
> you can use multiple-readers-single-writer without any locks on built-in
> types.
Not true. You can use multiple-readers, NO-writers without any locks,
however.
Chris
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