Boost C++ Libraries: Ticket #210: smart_ptr thread safe exception https://svn.boost.org/trac10/ticket/210 <pre class="wiki">Hi, Environment : WindowsXP boost 1_30_0 VC++6.0SP5 Context : I am running tests on lot of allocations/assigns between two smart pointers shared by two threads. Bug : I receive a "bad_weak_ptr" exception in the method sp_counted_base::add_ref() : void add_ref() { #if defined(BOOST_HAS_THREADS) mutex_type::scoped_lock lock(mtx_); #endif if(use_count_ == 0 &amp;&amp; weak_count_ != 0) boost::throw_exception(boost::bad_weak_ptr()); ++use_count_; ++weak_count_; } Test program is like : #include "boost/shared_ptr.hpp" class CTest { public: CTest( ) { } virtual ~CTest( ) { } void dump( ) { // do nothing. } }; typedef boost::shared_ptr&lt; CTest &gt; CTestPtr; CTestPtr ptr1; CTestPtr ptr2; // The class AThread is just a little wrapper around // the "_beginthreadex" function call, // it allows run/cancel/kill on threaded functions. void thfn1( AThread *threadwrapper, void * ) { // Loop until the "threadwrapper" is cancelled. while( !threadwrapper-&gt;isCanceled( ) ) { // Dummy work to access // concurrently the smart pointers... ptr1 = CTestPtr( new CTest ); ptr1 = CTestPtr( new CTest ); ptr1 = CTestPtr( new CTest ); CTestPtr p1( ptr1 ); CTestPtr p2( ptr2 ); CTestPtr p3( ptr1 ); CTestPtr p4( ptr1 ); ptr1 = CTestPtr( new CTest ); ptr1 = CTestPtr( new CTest ); ptr1 = CTestPtr( new CTest ); ptr1-&gt;dump( ); ::Sleep( rand( ) % 10 ); } } void thfn2( AThread *threadwrapper, void * ) { // Loop until the "threadwrapper" is cancelled. while( !threadwrapper-&gt;isCanceled( ) ) { ptr2 = ptr1; ::Sleep( rand( ) % 5 ); ptr2 = CTestPtr( new CTest ); ptr2 = CTestPtr( new CTest ); ptr2 = CTestPtr( new CTest ); CTestPtr p1( ptr2 ); CTestPtr p2( ptr2 ); CTestPtr p3( ptr2 ); CTestPtr p4( ptr2 ); ptr2 = CTestPtr( new CTest ); ptr2 = CTestPtr( new CTest ); ptr2 = CTestPtr( new CTest ); ::Sleep( rand( ) % 15 ); ptr1 = ptr2; } } int main( int argc, char* argv[ ] ) { // You can chage the amount of threads // to throw the exception. const int nThreads = 12; AThread *tab[ nThreads ]; // Create an array of threads. for( int i = 0; i &lt; nThreads; i+=2 ) { tab[ i ] = new AThread( thfn1 ); tab[ i + 1 ] = new AThread( thfn2 ); } // Start all the threads. for( i = 0; i &lt; nThreads; ++i ) { tab[ i ]-&gt;run( ); } // Sleep during 5 minutes. ::Sleep( 300000 ); // Ask the threads to cancel. for( i = 0; i &lt; nThreads; ++i ) { tab[ i ]-&gt;cancel( ); tab[ i ]-&gt;join( ); delete tab[ i ]; } return 0; } I can give you a little project to test it. </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/210 Trac 1.4.3 Peter Dimov Wed, 29 Oct 2003 12:10:52 GMT <link>https://svn.boost.org/trac10/ticket/210#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/210#comment:1</guid> <description> <pre class="wiki">Logged In: YES user_id=305912 Your threads write to 'ptr1' simultaneously without a lock. This is not supported. See http://www.boost.org/libs/smart_ptr/shared_ptr.htm#ThreadS afety </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Peter Dimov</dc:creator> <pubDate>Sun, 27 Jun 2004 15:54:19 GMT</pubDate> <title>status changed https://svn.boost.org/trac10/ticket/210#comment:2 https://svn.boost.org/trac10/ticket/210#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> </ul> Ticket