Boost C++ Libraries: Ticket #7066: Thread: An attempt to fix current_thread_tls_key static initialization order https://svn.boost.org/trac10/ticket/7066 <p> Currently Boost.Thread's Win32 TLS implementation uses a static variable (<code>thread::current_thread_tls_key</code>) that is not defined at a function scope. </p> <p> This has a certain shortcoming: As initilization order of global static variables is not predictable, it may happen that <code>thread::current_thread_tls_key</code> is used (via <code>thread::set_current_thread_data()</code>) before it is actually initialized by compiler (or the underlaying OS, to be precise). (An example: class that uses <code>thread_specific_ptr</code> and is not heap allocated.) </p> <p> Potentially any prior versions to Boost 1.50.0 are also affected, as 1.50.0 merely changed the initial value from a zero to a non-zero value. </p> <p> A proposed solution is to move <code>thread::current_thread_tls_key</code> inside a function scope (inside a helper function that sole purpose is to return reference to that variable). This ensures that <code>current_thread_tls_key</code> is initiliazed properly before it's used for the first time. As <code>thread::set_current_thread_data()</code> uses a static <code>current_thread_tls_init_flag</code> Boost.Once flag that is currently initialized at a global scope, move that flag too inside a function scope. </p> <p> A patch that implements proposed solution is attached. Unfortunaly, currently no test case exists (not sure if one is too easy to be made; At least on MSVC9 global static variables are initialized before the program's entry point inside a simple loop). </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7066 Trac 1.4.3 Pekka Seppänen <pekka.seppanen@…> Tue, 03 Jul 2012 06:26:21 GMT attachment set https://svn.boost.org/trac10/ticket/7066 https://svn.boost.org/trac10/ticket/7066 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost_1_50_0_current_thread_tls_key.patch</span> </li> </ul> <p> Move TLS related global static variables inside a function scope in order to ensure predictable initialization order. </p> Ticket Anthony Williams Tue, 03 Jul 2012 08:13:52 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/7066#comment:1 https://svn.boost.org/trac10/ticket/7066#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Fixed on trunk revision 79239 </p> Ticket viboes Tue, 03 Jul 2012 17:10:51 GMT milestone changed https://svn.boost.org/trac10/ticket/7066#comment:2 https://svn.boost.org/trac10/ticket/7066#comment:2 <ul> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.51.0</span> </li> </ul> Ticket