【发布时间】:2016-12-23 03:10:07
【问题描述】:
在图书馆的某个地方,我有一个看起来像的函数 -
inline int getIword()
{
static int i = std::ios_base::xalloc();
return i;
}
现在您可以阅读有关std::ios_base::xalloc() 调用here 的信息,但我想从提及的链接中强调这一行 -
这个函数是线程安全的;多个线程的并发访问不会导致数据竞争。 (C++14 起)
上面写着“自 C++14 起”,但我也需要 C++11 支持。由于函数调用实际上是在初始化 getIword() 方法的静态局部变量,并且我们知道 local static variable initialization is thread safe 用于 C++11 代码,因此假设此代码是安全的 -
如果仅对函数进行后续读取调用,则安全,例如。
auto something = getIword().安全吗?如果代码如下所示:
...
operator<<(std::ostream &os, T const value)
{
if (value == ...) {
os.iword(getIword()) = 1;
} else if (value == ...) {
os.iword(getIword()) = 0;
}
return os;
}
...
如果在后面的示例中不安全,我应该将 lock_guards 放在哪里以使其对 C++11 安全?围绕return i 或整个方法或在哪里进行调用?
【问题讨论】:
-
检查您的 C++ 实现是否已经是线程安全的可能是值得的。 保证在 C++14 中是新的,但对于实际实现来说很常见,可以尽早提供这些细节。
-
@MSalters 关于线程安全静态初始化的保证可以追溯到 C++11。
-
@NathanOliver MSalters 可能在谈论该功能的保证。
标签: c++ multithreading c++11 static thread-safety