id,summary,reporter,owner,description,type,status,milestone,component,version,severity,resolution,keywords,cc 5673,Quick allocator error in multi thread,Yury Podpruzhnikov ,Peter Dimov,"Compiler: '''MSVS 2008''' use macros '''BOOST_SP_USE_QUICK_ALLOCATOR''' Exception (Access vialation by reading address) occured by constructor of '''shared_ptr'''. Cause of exception is multi thread creation of static mutex in quick allocator. shared_ptr call quick allocator. quick allocator use next code: {{{ lightweight_mutex::scoped_lock lock( mutex() ); }}} function '''mutex()''' return internal static variable: {{{ boost_1_44_0\boost\smart_ptr\detail\quick_allocator.hpp template struct allocator_impl { ... static lightweight_mutex & mutex() { static freeblock< sizeof( lightweight_mutex ), boost::alignment_of< lightweight_mutex >::value > fbm; static lightweight_mutex * pm = new( &fbm ) lightweight_mutex; return *pm; } ... } }}} 2 thread call mutex() (first time for this instantiation of template class allocator_impl). One start init '''pm''' and go to sleep. Other check that '''pm''' is initialized and return its value (in my case NULL) and raise exception. [[BR]] Initialization of static variable in C++ is not safe for multi thread. This code is incorrect. In dependence from realization of static variable you will be have access vialation or memory leak - flag of static variable initialization (construction) set before or after initialization. [[BR]] In MSVS 2008 flag set before and you take access vialation. [[BR]][[BR]] '''Decision''' [[BR]] Not resolve, but minimize problem. [[BR]] Idea:[[BR]] As I understand, This problem can appear in first initialization of __every__ instantiation of shared_ptr - first use __every__ instantiation of allocator_impl. [[BR]] If you synchronize of initialization '''pm''' then problem can appear only first initialization of __any__ instantiation of shared_ptr. And I can create first fictive shared_ptr in non-multi thread and then normal use shared_ptr in multi thread. [[BR]] Realization:[[BR]] {{{ boost_1_44_0\boost\smart_ptr\detail\quick_allocator.hpp lightweight_mutex & global_mutex() { static freeblock< sizeof( lightweight_mutex ), boost::alignment_of< lightweight_mutex >::value > fbm; static lightweight_mutex * pm = new( &fbm ) lightweight_mutex; return *pm; } template struct allocator_impl { static lightweight_mutex & mutex() { static freeblock< sizeof( lightweight_mutex ), boost::alignment_of< lightweight_mutex >::value > fbm; static lightweight_mutex * pm = NULL; if(pm == NULL) { lightweight_mutex::scoped_lock lock( global_mutex() ); if(pm == NULL) { pm = new( &fbm ) lightweight_mutex; } } return *pm; } } }}} ",Bugs,new,To Be Determined,smart_ptr,Boost 1.44.0,Problem,,,