Boost C++ Libraries: Ticket #2939: Possible thread-safety issue with Boost Function https://svn.boost.org/trac10/ticket/2939 <p> Hi, I realize this might slip through the mailing lists so I am posting it here: </p> <p> <a class="ext-link" href="http://article.gmane.org/gmane.comp.lib.boost.devel/188442"><span class="icon">​</span>http://article.gmane.org/gmane.comp.lib.boost.devel/188442</a> </p> <p> I was just looking through the Boost Function source and noticed the following in assign_to: </p> <pre class="wiki"> // Note: it is extremely important that this initialization use // static initialization. Otherwise, we will have a race // condition here in multi-threaded code. See // http://thread.gmane.org/gmane.comp.lib.boost.devel/164902/. static vtable_type stored_vtable = { { &amp;manager_type::manage }, &amp;invoker_type::invoke }; </pre><p> However, my copy of the C++ standard seems to say this regarding local statics in section 6.7: </p> <p> The zero-initialization (8.5) of all local objects with static storage duration (3.7.1) is performed before any other initialization takes place. A local object of POD type (3.9) with static storage duration initialized with constant-expressions is initialized before its block is first entered. An implementation is permitted to perform early initialization of other local objects with static storage duration under the same conditions that an implementation is permitted to statically initialize an object with static storage duration in namespace scope. </p> <p> I read this to mean that an implementation may decide to initialize stored_vtable statically or the first time the block is entered. So I think that this means we cannot rely on static initialization behaviour. </p> <p> If you agree, then a simple fix for this might just be: </p> <pre class="wiki">template&lt;typename VTable, typename Invoker, typename Manager&gt; struct vtable_static_constructor { static VTable vtable; }; template&lt;...&gt; VTable vtable_static_constructor&lt;V,I,M&gt;::vtable = { {&amp;Manager::manage, &amp;Manager::invoke } }; assign_to() { ... vtable_type &amp; stored_vtable = vtable_static_constructor&lt;vtable_type,invoker_type,manager_type&gt;::vtable; ... } </pre><p> If not, could you please point me to where in the standard this is guaranteed to be statically initialized? It would help me decide whether or not to fix some other code that has a similar pattern, if nothing else. </p> <p> Thanks in advance! </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2939 Trac 1.4.3