本文介绍了我如何知道C ++编译器是否生成线程安全的静态对象代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GCC中,本地静态变量是线程安全的(通过特殊函数),除非给出编译器选项。



同样,MSVC 2015及以后的版本支持相同的功能,可以通过。

是否有任何宏或其他功能,如 __ EXCEPTIONS __ GXX_RTTI 检查编译阶段是否启用了这些功能?我认为检查 __ cplusplus _MSC_VER 不会帮助。

解决方案

看起来有一个定义 __ cpp_threadsafe_static_init

CLang - ()



GCC -



MSVC - 调查中的功能请求



对cppreference.com有用:







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.

解决方案

Looks like there is one define __cpp_threadsafe_static_init.

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:

这篇关于我如何知道C ++编译器是否生成线程安全的静态对象代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 10:46