Is initialization of local static function-object thread-safe?

五迷三道 提交于 2019-12-04 05:39:20

"The most common compilers" probably differ on this, as they have not all the same support for C++11.

In C++11 the initialization of the static variable is thread safe. In C++03 it is not (as there aren't any threads according to the standard).

As Bo says, the current C++ standard demands that static variable initialization not introduce a data race. (This is of course only relevant to the dynamic initialization phase.) For example, if you look at the output of GCC when initializing a static variable, you'll indeed find calls to __cxa_guard_acquire, __cxa_guard_release and __cxa_guard_abort surrounding the initialization.

The Itanium C++ ABI actually formalizes the mechanism.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!