Subject: [Boost-bugs] [Boost C++ Libraries] #5673: Quick allocator error in multi thread
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2011-07-06 12:48:01
#5673: Quick allocator error in multi thread
----------------------------------------------------+-----------------------
Reporter: Yury Podpruzhnikov <QWERTYura@â¦> | Owner:
Type: Bugs | Status: new
Milestone: To Be Determined | Component: None
Version: Boost 1.44.0 | Severity: Problem
Keywords: |
----------------------------------------------------+-----------------------
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<unsigned size, unsigned align_> 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<unsigned size, unsigned align_> 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;
}
}
}}}
-- Ticket URL: <https://svn.boost.org/trac/boost/ticket/5673> Boost C++ Libraries <http://www.boost.org/> Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:06 UTC