
I thought boost::shared_ptr was lock-free and thread safe on the major
platforms supported by boost. I could > be wrong though. The reference counter is lock-free and thread safe. The shared_ptr itself is not shared_ptr<A> mutualA(new A); mutex mutualAlock; void * thread_func(void*){ shared_ptr<A> isolatedA; { //accessing mutualA requires lock lock(mutualAlock); isolatedA=mutualA; } //accessing isolatedA is fine //this only depends on the ref counter being thread safe vector<shared_ptr<A> > vA(100,isolatedA); } Also, it should be clear that this is unsafe: void * thread_func2(void*){ mutualA->doSomething(); } Since another thread is likely to do void * thread_func3(void*){ mutualA.reset(new A);//oops just deleted object thread_func2 was using } In the pointer I have, the above code would be linear_mutual_ptr<A> mutualA(new A); //for the shared instance void * thread_func_l(void*){ linear_ptr<A> isolatedA=mutualA;//for the isolated instance //isolatedA now has a copy, access like shared_ptr sans weak_ptr vector<linear_ptr<A> > vA(100,linearA); //and FWIW this works vector<linear_mutual_ptr<A> > vAm(100,mutualA); //but copies of linear_mutual_ptr are not as efficient //AND of course not every element may actually //points to the same object } void * thread_func2_l(void*){ //ensure that the object is going to hang around long enough //to complete the operation linear_ptr<A>(mutualA)->doSomething(); } linear_mutual_ptr also has a bool compare_and_swap(linear_ptr<T> const &oldval, linear_ptr<T> const &newval)volatile Operation, so that simple STM systems can be built using it, i.e. void * thread_func_l2(void*){ do{ linear_ptr<A> isolatedA=mutualA; linear_ptr<A> isolatedA2(new A(*isolatedA)); //not shown, but A's operator new has a lock-free allocator attached isolatedA2->modify(); if(mutualA.compare_and_swap(isolatedA,isolatedA2))return 0; }while(true); } Lance -----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Patrick Twohig Sent: Monday, April 21, 2008 12:37 PM To: boost@lists.boost.org Subject: Re: [boost] Nonlocking data structures. I thought boost::shared_ptr was lock-free and thread safe on the major platforms supported by boost. I could be wrong though. _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost Visit our website at http://www.ubs.com This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. E-mails are not encrypted and cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message which arise as a result of e-mail transmission. If verification is required please request a hard-copy version. This message is provided for informational purposes and should not be construed as a solicitation or offer to buy or sell any securities or related financial instruments.