std :: atomic_flag作为成员变量 [英] std::atomic_flag as member variable
问题描述
在类构造函数中初始化 std :: atomic_flag
的安全方法是什么?
What is a safe way to initialize an std::atomic_flag
in a class constructor?
这个问题 似乎在问我同样的问题-除了在此询问者抱怨编译器问题外。
This question seems to be asking the same question I'm asking - except here the asker is complaining about a compiler problem.
我的问题与C ++标准本身有关。根据 此网站 ,初始化<$ c未指定使用构造函数初始化程序语法的$ c> std :: atomic_flag 。
My question relates to the C++ standard itself. According to this site, initializing an std::atomic_flag
using constructor initializer syntax is unspecified.
std::atomic_flag static_flag = ATOMIC_FLAG_INIT; // static initialization,
// guaranteed to be available during dynamic initialization of static objects.
int main()
{
std::atomic_flag automatic_flag = ATOMIC_FLAG_INIT; // guaranteed to work
// std::atomic_flag another_flag(ATOMIC_FLAG_INIT); // unspecified
}
此信息正确吗?如果是这样,我认为:
Is this information correct? If so, I assume that:
struct Foo
{
Foo() : flag(ATOMIC_FLAG_INIT)
{ }
std::atomic_flag flag;
};
...也未指定。因此,这是否意味着我们不能使用 std :: atomic_flag
作为类的成员变量?还是从类构造函数中简单调用 std :: atomic_flag :: clear()
是安全的吗?
...is also unspecified. So, does this mean that we can't use an std::atomic_flag
as a member variable of a class? Or is it safe if we simple call std::atomic_flag::clear()
from within a class constructor?
推荐答案
从N3337到N3936(当前的C ++ 14草案)以来,与 ATOMIC_FLAG_INIT
的使用有关的措词已更改。前者在示例中显示了 ATOMIC_FLAG_INIT
宏在复制初始化上下文中的可能用法,这是非规范性的,并且未提及有关内部使用的任何内容。其他初始化上下文。
The wording concerning use of ATOMIC_FLAG_INIT
has changed since N3337 to N3936 (the current C++14 draft). The former shows a possible usage, in a copy-initialization context, of the ATOMIC_FLAG_INIT
macro in an example, which are non-normative, and doesn't mention anything about uses within other initialization contexts.
N3936阐明了用法,不再作为示例列出复制初始化用法,而是作为描述本身的一部分。
N3936 clarifies the usage, and no longer lists the copy-initialization usage as an example, but as part of the description itself.
§29.7/ 4 [atomics.flag]
宏
ATOMIC_FLAG_INIT
的定义方式应使其可用于
初始化atomic_flag
类型的对象到清晰的状态。该宏可以
的形式使用:
The macro
ATOMIC_FLAG_INIT
shall be defined in such a way that it can be used to initialize an object of typeatomic_flag
to the clear state. The macro can be used in the form:
atomic_flag guard = ATOMIC_FLAG_INIT;
尚不确定该宏是否可以在其他初始化上下文中使用。对于完整的静态持续时间对象,该初始化应为静态。除非使用 ATOMIC_FLAG_INIT
初始化,否则无法确定 atomic_flag
对象的初始状态为set还是clear。
It is unspecified whether the macro can be used in other initialization contexts. For a complete static-duration object, that initialization shall be static. Unless initialized with ATOMIC_FLAG_INIT
, it is unspecified whether an atomic_flag
object has an initial state of set or clear.
The rationale for these changes is discussed here.
所以您说对了,不能依赖在成员初始值设定项列表中使用宏。解决方案是使用非静态数据成员初始化程序或
brace-or-equal-initializer 初始化 atomic_flag
。然后将在副本初始化上下文中对其进行初始化。
So you're right that using the macro in the member initializer list cannot be relied upon. The solution is to use a non-static data member initializer, or brace-or-equal-initializer, to initialize the atomic_flag
. Then it will be initialized in a copy initialization context.
struct Foo
{
std::atomic_flag flag = ATOMIC_FLAG_INIT;
};
这篇关于std :: atomic_flag作为成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!