Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2007-08-06 11:11:12


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.


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