when are static POD variables from constant expressions initializated inside functions?
===

I have a question about static variables inside functions. I basically want to know if static variables that are POD types, and are initialized by a constant expression, are initialized before the function runs the first time or at static initialization phase at program startup.

Let me put an example:

    #include <iostream>
    #include <string>
    using namespace std;
    
    // forward declaration
    extern int statically_initialized_global;

    // helper printer class
    class printer
    {
        string str_;
    public:
        printer(const char* str) : str_(str){cout<<"Constructing "<<str_<<". Statically initialized global: "<<statically_initialized_global<<endl;}
        ~printer(){cout<<"Destructing "<<str_<<endl;}
    };

    // global object initialized in the dynamic initialization phase (after static initialization phase)
    printer dynamically_initialized_global = "Global string";

    // global variable initialized in the static initialization phase (before dynamic initialization)
    int statically_initialized_global = 4;

    // A test function
    void function()
    {
        /* static variable initialized when? in the first use? or in the static initialization phase.
        I THINK that it would be initialized in the static initialization phase (at program startup),
        but it could also get initialized when the main block enters.
        It makes a big difference (read the explanation) */
        static int statically_initialized_static = 1;
        if(statically_initialized_static)
        {
            statically_initialized_static = 0;
        }
        
        // static object initialized in the first use
        static printer locally_initialized_static = "Local string";
    }

    int main()
    {
        function();
    }

Now, why is it important? If it is initialized at system startup, I can write this code:

    #include <winbase.h>

    /* Defined somewhere. Must be called only once */
    bool once();

    /* This funciton must call once() only once. It may be called from different threads at the same time (even the first time) */
    bool call_once_only_once()
    {
        static volatile bool initialized = false;
        static LONG initializer = TRUE;

    	/* Return value */
    	static bool ret;
		
    	/* Read the value of initializer to see if I'm the initializer.
		At the same time (atomically) write FALSE, because the next will not be the initializer. */
    	if(InterlockedExchange(&initializer,FALSE))
    	{
    		/* Call the function */
    		ret = once();
    		
    		/* Finished initializing. */
    		initialized = true;
    	}
    	else
    	{
    		/* Else, if it was FALSE, it means that somebody else came first and was the initializer.
    		Now I'll wait until it's safe to use the ret value, because the other thread could
		have not finished initializing it yet. */
    		while(!initialized)
    		{
    			Sleep(1);
    		}
    	}
    	return ret;
    }

Otherwise, I must write:

    #include <winbase.h>

    /* Defined somewhere. Must be called only once */
    bool once();

    /* Helpers */
    static volatile bool initialized = false;
    static LONG initializer = TRUE;

    /* This funciton must call once() only once. It may be called from different threads at the same time (even the first time) */
    bool call_once_only_once()
    {
    	/* Return value */
    	static bool ret;
		
    	/* Read the value of initializer to see if I'm the initializer.
		At the same time (atomically) write FALSE, because the next will not be the initializer. */
    	if(InterlockedExchange(&initializer,FALSE))
    	{
    		/* Call the function */
    		ret = once();
    		
    		/* Finished initializing. */
    		initialized = true;
    	}
    	else
    	{
    		/* Else, if it was FALSE, it means that somebody else came first and was the initializer.
    		Now I'll wait until it's safe to use the ret value, because the other thread could
		have not finished initializing it yet. */
    		while(!initialized)
    		{
    			Sleep(1);
    		}
    	}
    	return ret;
    }

I mean, imagine that the variables _initialized_ and _initializer_ are initialized the first time the block is entered. If so, there will be a flag similar to this (implemented by the compiler) at function block startup (there will actually be a mask instead of many bools, but I'll write bools just for clarity):

    static bool initialized_INITIALIZED = false;
    if(!initialized_INITIALIZED)
    {
        initialized = false;
        initialized_INITIALIZED = true;
    }
    static bool initializer_INITIALIZED = false;
    if(!initializer_INITIALIZED)
    {
        initializer = TRUE;
        initializer_INITIALIZED = true;
    }

The problem is that if two threads enter at the same time, both could be the initializer. Imagine that thread1 enters if(!initializer_INITIALIZED) block, but yields before initializer is set to TRUE. Then thread 2 executes, and sets initializer to TRUE and calls once() function. Then it yields and thread1 awakes and sets initializer to TRUE again and calls once() function **for the second time**.

**¿Do you get the problem?**

**So, anyone knows when static POD variables inside functions are initialized (from constant expressions)?**