Controls the addition of extra code in the binary to ensure that multiple threads cannot enter a static local initialization at the same time.
#pragma thread_safe_init on | off | reset
A C++ program that uses multiple threads and static local initializations introduces the possibility of contention over which thread initializes static local variable first. When the pragma is on, the compiler inserts calls to mutex functions around each static local initialization to avoid this problem. The C++ runtime library provides these mutex functions.
int func(void) { // There may be synchronization problems if this function is // called by multiple threads. static int countdown = 20; return countdown--; }
The following listing shows another example.
#pragma thread_safe_init on void thread_heavy_func() { // Multiple threads can now safely call this function: // the static local variable will be constructed only once. static std::string localstring = thread_unsafe_func(); }
This pragma does not correspond to any panel setting. By default, this pragma is off.