是否初始化本地静态函数对象线程安全? [英] Is initialization of local static function-object thread-safe?

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

问题描述

以下两个函数产生不同的程序集,这告诉我他们是不同的。有人能告诉我他们有什么不同吗?并且是函数本地静态变量初始化在func2线程安全还是不?如果答案取决于编译器,我想知道最常见的编译器如何与func2行为。

The following two functions produce different assemblies, which tells me they're different. Can someone tell me in what way they are different? And is the function local static variable initialization in func2 thread-safe or not? If the answer depends on the compiler, I'd like to know how would the most common compilers behave with func2.

int func1(int val)
{
    const auto impl = [](int v)
    {
        return v * 10;
    };

    return impl(val);
}

int func2(int val)
{
    static const auto impl = [](int v)
    {
        return v * 10;
    };

    return impl(val);
}


推荐答案

最常用的编译器可能不同,因为他们没有对C ++ 11的所有相同的支持。

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

在C ++ 11中,静态变量的初始化是线程安全的。在C ++ 03它不是(因为没有根据标准的任何线程)。

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

这篇关于是否初始化本地静态函数对象线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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