Thread safety and weak_ptr questions

Hello struct A {} // executes before spawning any boost threads shared_ptr<A> sp(new A()); weak_ptr<A> wp(sp); // then in thread1 sp.reset(); // in thread thread2 shared_ptr<A> sp2(wp.lock()); Is the above code thread safe ? Is sp2 always either a default initilized shared_ptr<A> or a valid shared_ptr to the instance of A initially referenced by sp ? I couldn't find documentation on weak_ptr thread safety issues just on shared_ptr. I am unable to determine from the documentation what is the intended behaivour for the above code. I could look into the boost code and/or test it but I want to know not how it works but how it's intended to work (portably and all) :) Thank you. -- Mihai RUSU Email: dizzy@roedu.net GPG : http://dizzy.roedu.net/dizzy-gpg.txt WWW: http://dizzy.roedu.net "Linux is obsolete" -- AST

Dizzy wrote:
Hello
struct A {}
// executes before spawning any boost threads shared_ptr<A> sp(new A()); weak_ptr<A> wp(sp);
// then in thread1 sp.reset();
// in thread thread2 shared_ptr<A> sp2(wp.lock());
Is the above code thread safe ?
Yes, it is. You are manipulating different variables in the two threads. The rules for weak_ptr are essentially the same as those for shared_ptr.
participants (2)
-
Dizzy
-
Peter Dimov