
I need to ensure that certain objects (specifically the policy bundles which manage singleton instances) are created in a thread-safe manner. First question: Can two threads both initialize the same static variable if they pass through the same function at the same time? Ex: void foo ( ) { static Thing t; } Can t's constructor be called twice by two threads executing foo? If so, I feel that I need to construct my policy bundles before execution enters main, a time when it is pretty much guaranteed that the application is operating in a single-threaded context. I thought that this would be simple to do: just write a class that calls the function with the static variable in its constructor and create a global instance of that class. However, the standard appears to say that initialization of such global variables can happen after main is entered, so long as it happens before the global variable is used. If the program starts spawning threads before the global variable is constructed I would still have the same problem. How can I ensure that initialization is performed before main is entered? If I cannot, is there some other way to make initialization of static variables thread-safe? I cannot lock a mutex to manage this, because the mutex lives inside the threading policy instance and the threading policy instance lives in the policy bundle that I am trying to create in the first place. -Jason