Boost logo

Boost Users :

From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2007-08-06 11:08:46


On Mon, August 6, 2007 16:56, gast128 wrote:
>
> This should mean that shared_ptr is not at all thread safe, as in above race
> condition. I am not sure if this contradicts the quote 'shared_ptr objects
> offer the same level of thread safety as built-in types', it at least does not
> address the thread safety of the unique reference counting feature (which has
> no analog for 'normal' pointers).
>
> The atomic increment and decrement ref counting then only work for a shared
> const shared pointer:
>
> void g(boost::shared_ptr<const B> ptr)
> {
> //...
> }
>
> void f()
> {
> boost::shared_ptr<const B> ptr(new B);
>
> //give ptr to threads
> boost::thread thrd1(boost::bind(&g, ptr));
> boost::thread thrd2(boost::bind(&g, ptr));
>
> //fire and forget
> ptr.reset();
> }
>
> However they finally end up in a dtor call which according to the
> documentation is an legal or illegal write operation.

Well, since the standard requires all parameters to function be evaluated before the function
call, your code is safe. So before the thread ctor is called, all binds are evaluated and have
already a copied instance of shared_ptr instance, is not it so? And for sure you can afterwards
safely call reset, or just discard the shared pointer which will decrement the shared counter.

With Kind Regards,

Ovanes Markarian


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