|
Boost Users : |
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2007-08-06 11:22:39
On Mon, August 6, 2007 17:11, Peter Dimov wrote:
> gast128 wrote:
>
>> The atomic increment and decrement ref counting then only work for a
>> shared const shared pointer:
>
> No, the presence or absence of 'const' do not affect the thread safety of a
> piece of code.
>
>> 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();
>> }
>
> This example is OK. You have three distinct shared_ptr variables, all named
> 'ptr' (one per thread). Despite them having the same name, they are still
> different objects, not shared among threads, so you can assign/reset/destroy
> them at will.
>
Yes, just in addition, your code would be broken, if g would expect a shared_ptr by reference,
i.e. void g(boost::shared_ptr<const B>& ptr). Then all your threads would own the same shared_ptr
instance and the thread in which runs the function f would reset the pointer which might be used
by thrd1 or thrd2 and that might cause the scenario from the docs example.
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