
6 Aug
2007
6 Aug
'07
3:11 p.m.
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.