Boost logo

Boost :

Subject: [boost] [thread] thread_specific_ptr and static initialization order
From: Tanguy Fautré (tfautre_at_[hidden])
Date: 2010-05-08 14:56:54


Hi,

After upgrading from 1.42 to 1.43 (using VC10 x64), I encounter an
access violation related to thread_specific_ptr. The program used to run
fine with previous version of Boost on several platforms (Win x32, Win
x64 and Linux x64).

I've managed to create a small program that reproduces the problem. It
seems to be a static initialization order related problem. If the TSS is
initialized by a static object, the program fails (nullptr exception).
However, if the static object is commented out, and the TSS is solely
initialized in the main, the program runs fine.

Is thread_specific_ptr supposed to work even when initialized by another
static object? Or does it require to be initialized only after entering
main?

Regards,

Tanguy

#include <boost/thread/thread.hpp>
#include <boost/thread/tss.hpp>

#include <iostream>
#include <stdexcept>

namespace
{

     boost::thread_specific_ptr<int> tss;

     void verifyTss()
     {
         if (tss.get() == nullptr)
             throw std::runtime_error("verifyTss failed (nullptr)");
     }

     class InitTss
     {
     public:

         InitTss()
         {
             tss.reset(new int);
         }
     };

     InitTss init_tss;

}

int main(int argc, char argv[])
{
     try
     {
         tss.reset(new int);

         verifyTss();

         boost::thread thread([] ()
         {
             tss.reset(new int);
         });
         thread.join();

         verifyTss();
     }

     catch (const std::exception & error)
     {
         std::cerr << "ERROR: " << error.what() << std::endl;
     }
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk