VC2010是静态init线程安全吗? [英] Is static init thread-safe with VC2010?

查看:186
本文介绍了VC2010是静态init线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找所有的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的 Static 上的文档:


在多线程应用程序中的静态局部变量不是线程安全的,我们不推荐它作为一个编程实践。


第二部分您的问题有一些很好的现有答案。 p>

2015年11月22日更新:



其他人已经验证,具体来说,静态初始化不是线程安全的


您可以在VS2015上想添加一个VS2015终于正确的: https://msdn.microsoft .com / en-au / library / hh567368.aspx#concurrencytable (Magic statics)



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:

Assigning a value to a static local variable in a multithreaded application is not thread safe and we do not recommend it as a programming practice.

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:

you may want to add that VS2015 finally gets it right: https://msdn.microsoft.com/en-au/library/hh567368.aspx#concurrencytable ("Magic statics")

这篇关于VC2010是静态init线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆