In GCC, local static variable is thread-safe (by special function__cxa_guard_acquire
) unless -fno-threadsafe-statics
compiler option is given.
Similarly, MSVC 2015 and onward version support the same feature and can be disabled by /Zc:threadSafeInit-
.
Is there any macro or other features, like __EXCEPTIONS
or __GXX_RTTI
to check on compilation stage if such features are enabled or not? I think checking __cplusplus
or _MSC_VER
won't help.
No, static functions are not inherently thread-safe.
A threadsafe function protects shared resources from concurrent access by locks. Thread safety concerns only the implementation of a function and does not affect its external interface.
Static variables are not thread safe. Instance variables do not require thread synchronization unless shared among threads. But, static variables are always shared by all the threads in the process. Hence, access to static variable is not thread safe.
Starting in C++11, scoped static initialization is now thread-safe, but it comes with a cost: Reentrancy now invokes undefined behavior.] The rule for static variables at block scope (as opposed to static variables with global scope) is that they are initialized the first time execution reaches their declaration.
In multithreaded environments, we need to write implementations in a thread-safe way. This means that different threads can access the same resources without exposing erroneous behavior or producing unpredictable results. This programming methodology is known as “thread-safety”.
The /Zc:threadSafeInit compiler option tells the compiler to initialize static local (function scope) variables in a thread-safe way, eliminating the need for manual synchronization. Only initialization is thread-safe. Use and modification of static local variables by multiple threads must still be manually synchronized.
So, another common approach that we can use for achieving thread-safety is implementing synchronized methods. Simply put, only one thread can access a synchronized method at a time while blocking access to this method from other threads. Other threads will remain blocked until the first thread finishes or the method throws an exception.
If this is a problem in your code, especially in code that must run on older operating systems, use /Zc:threadSafeInit- to disable the thread-safe initialization code. For more information about conformance issues in Visual C++, see Nonstandard Behavior. Open the project's Property Pages dialog box.
Looks like there is one define __cpp_threadsafe_static_init
.
SD-6: SG10 Feature Test Recommendations:
C++11 features
Significant features of C++11
Doc. No. Title Primary Section Macro name Value Header
N2660 Dynamic Initialization and Destruction with Concurrency 3.6
__cpp_threadsafe_static_init
200806 predefined
CLang - http://clang.llvm.org/cxx_status.html#ts (github.com)
GCC - https://gcc.gnu.org/projects/cxx-status.html
MSVC - Feature request under investigation https://developercommunity.visualstudio.com/content/problem/96337/feature-request-cpp-threadsafe-static-init.html
Useful on cppreference.com:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With