### thread_safe_init

Controls the addition of extra code in the binary to ensure that multiple threads cannot enter a static local initialization at the same time.

##### Syntax

#pragma thread_safe_init on | off | reset

##### Targets

All platforms.

##### Remarks

When C++ programs use static local initializations, like this:

int func() {
static int countdown = 20;
return countdown--;
}

the static locals are initialized the first time the function is executed. As such, if multiple threads are running at the same time, and two of them happen to enter the function at the same time, there will be contention over which one initializes the variable.

When this pragma is on, the compiler inserts a mutex around the initialization to avoid this problem as shown below:

NOTE This requires runtime support which may not be implemented on all platforms, due to the possible need for operating system support.

#pragma thread_safe_init on
void thread_heavy_func()
{
// multiple routines cannot enter at the same time
static std::string localstring = thread_unsafe_func();
}

NOTE When an exception is thrown from a static local initializer, the initializer is retried by the next client that enters the scope of the local.

This pragma does not correspond to any panel setting. The default setting is off.