问题描述
我一直在寻找所有的SO和MSDN回答这个问题,但似乎找不到一个明确的最终答案...
我知道它是在C ++ 11标准和当前GCC版本表现这样,但VC2010目前保证本地静态变量初始化的线程安全?
ie:这是VC2010的线程安全吗?
static S& getInstance()
{
static S instance;
return instance;
}
...如果没有,线程安全的单例在C ++中用VC2010实现?
编辑:正如Chris Betti的回答所指出的,VC2010没有实现线程安全的本地静态变量init。
从Visual Studio 2010的上的文档:
第二部分您的问题有。 p>
2015年11月22日更新:
其他人已经验证,具体来说,静态初始化不是线程安全的
I've been looking all around SO and MSDN for an answer to this question, but cannot seem to find a clear and final answer...
I know that it's in the C++11 standard and that current GCC version behave this way, but does VC2010 currently guarantees thread-safety of a local static variable initialization?
i.e.: Is this thread-safe with VC2010?
static S& getInstance()
{
static S instance;
return instance;
}
...And if not, what is the current best practice to get a thread-safe singleton implementation in C++ with VC2010?
EDIT: As pointed out by Chris Betti's answer, VC2010 doesn't implement thread-safeness of local static variable init.
From Visual Studio 2010's documentation on Static:
The second part of your question has some good existing answers.
Updated Nov 22, 2015:
Others have verified, specifically, that static initialization is not thread safe either (see comment and other answer).
User squelart on VS2015:
这篇关于VC2010是静态init线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!