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  
Remarks

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.

Listing: Static local initialization example
int func(void) {
  // There may be synchronization problems if this function is 

  // called by multiple threads.

  static int countdown = 20;

  return countdown--;

}
Note: This pragma requires runtime library functions which may not be implemented on all platforms, due to the possible need for operating system support.

The following listing shows another example.

Listing: Example thread_safe_init
#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();

}
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. By default, this pragma is off.