Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I know if C++ compiler make thread-safe static object code?

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.

like image 519
Byoungchan Lee Avatar asked Jun 12 '17 13:06

Byoungchan Lee


People also ask

Is static thread safe C?

No, static functions are not inherently thread-safe.

What is thread safe code in C?

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.

Are static class variables thread safe?

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.

Are static objects thread safe C++?

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.

What is thread safety in C++?

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”.

What is ZC thread safe initialization?

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.

How to achieve thread safety in Java?

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.

How do I disable thread safe initialization in Visual C++?

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.


1 Answers

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:

  • Feature Test Recommendations
  • C++ compiler support
like image 142
Mihayl Avatar answered Oct 21 '22 14:10

Mihayl