c++ - 初始化 std::atomic_bool?

标签 c++ c++11 visual-studio-2012 atomic

我想使用 std::atomic_bool 因为我想要一个应该被不同线程访问的 bool 值。

它是一个 static 成员变量。问题是我想用 false 作为第一个状态来初始化它。通常我会这样做: std::atomic_bool World::mStopEvent = false;

但问题似乎是它没有将 false 作为构造函数。那么我应该如何初始化这样一个变量呢? 我正在使用 VS 2012。

最佳答案

这是 known issue in Visual Studio 2012 (known as VC11) ,您应该对现有的 Connect 项目进行投票,以便 Microsoft 知道它会影响更多人,因为他们推迟了修复。

Hi,

Thanks for reporting this bug. I'm Microsoft's maintainer of the STL, and I wanted to let you know that while this bug remains active in our database, it won't be fixed in VC11 RTM (VS 2012 RTM). All bugs are important to us, but some are more severe than others and rise to the top of our priority queue.

I'm copying-and-pasting this response across all of the STL's active Connect bugs, but the following terse comments apply specifically to your bug:

  • Yep, we're missing these constructors on atomic_bool, atomic_int, etc. (atomic<bool>, atomic<int>, etc. have them). 29.5 [atomics.types.generic]/7 says "There shall be named types corresponding to the integral specializations of atomic, as specified in Table 145, and a named type atomic_bool corresponding to the specified atomic<bool>. Each named type is a either typedef to the corresponding specialization or a base class of the corresponding specialization. If it is a base class, it shall support the same member functions as the corresponding specialization." which makes me really want to use typedefs (1 type is always simpler than 2 types), but I'll need to see if that would introduce any other issues.

I can't promise when we'll be able to resolve this bug, but we hope to do so as soon as possible (and I'll send another response when that happens) - our first opportunity will be the "out of band" release between VC11 and VC12 that Herb Sutter announced at the GoingNative 2012 conference.

Note: Connect doesn't notify me about comments. If you have any further questions, please E-mail me.

Stephan T. Lavavej Senior Developer - Visual C++ Libraries stl@microsoft.com

基本上,您需要使用 std::atomic<T>暂时。

关于c++ - 初始化 std::atomic_bool?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15750917/

相关文章:

c++ - 我应该如何定义一个返回指针引用的函数? (引用指针与指向指针的指针)

c# - 从 C# 调用的 C++ 写入输出窗口

C++ 赋值副作用

c++ - 是否可以在 C++ 中声明另一个函数时使用函数返回类型作为参数?

c++ - 在可变参数模板递归基本情况下摆脱不必要的类

c++ - 为什么我在此 C++ 代码中得到 "Access violation"

html - "cannot resolve tag svg"在 Visual Studio 2012 中使用 html5

c++ - 类 header.h 不包含在 main 中

c++ - 将数组拆分为单独的正数组和负数组 C++

c++ - C++中的继承和多文件