thread_safe_init. NXP CodeWarrior Development Studio
تبلیغات
تبلیغات
Chapter 29 Pragmas for C++
template too complex or recursive
This pragma does not correspond to any panel setting.
29.42 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 29-12. 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.
Listing 29-13 on page 453 shows another example.
Listing 29-13. 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();
}
CodeWarrior Development Studio for Power Architecture Processors Build Tools Reference Manual, Rev.
10.6, 02/2014
Freescale Semiconductor, Inc.
453
دانلود
تبلیغات