简体   繁体   English

使用ATOMIC_FLAG_INIT和std :: atomic_flag :: clear有什么区别

[英]what is the difference between using ATOMIC_FLAG_INIT and std::atomic_flag::clear

Are the following two code snippets the same: 以下两个代码段是否相同:

std::atomic_flag lock = ATOMIC_FLAG_INIT;

and

std::atomic_flag lock;
lock.clear();

It seems like the second can allow for lock to be in an unknown state for a few clicks 似乎第二个可以允许锁定在几次点击时处于未知状态

Is the first code snippet always going to have a known state? 第一个代码片段是否总是具有已知状态?

ATOMIC_FLAG_INIT is an implementation defined macro that is guaranteed to work in expressions like the one you've posted. ATOMIC_FLAG_INIT是一个实现定义的宏,可以保证在你发布的表达式中工作。 It comes in handy for initializing an atomic_flag that you may have defined at namespace scope, for instance. 例如,它可以用于初始化您可能在命名空间范围内定义的atomic_flag It also guarantees that the flag will be cleared and that if the flag itself has static storage duration, the initialization will also be static. 它还保证标志将被清除,如果标志本身具有静态存储持续时间,则初始化也将是静态的。

The second set of statements is initialization followed by clearing of the flag. 第二组语句是初始化,然后是清除标志。 Since the state of atomic_flag is unspecified post default construction, it does mean that the flag is in an unspecified state until the clear() has been executed. 由于在默认构造之后未指定atomic_flag的状态,因此它确实意味着该标志处于未指定状态,直到执行了clear()

Yes (per 29.7[atomics.flag] §4 ): 是(按照29.7 [atomics.flag]§4 ):

The macro ATOMIC_FLAG_INIT shall be defined in such a way that it can be used to initialize an object of type atomic_flag to the clear state. 应以这样的方式定义宏ATOMIC_FLAG_INIT ,使其可用于将atomic_flag类型的对象初始化为清除状态。 For a static-duration object, that initialization shall be static. 对于静态持续时间对象,该初始化应该是静态的。 It is unspecified whether an uninitialized atomic_flag object has an initial state of set or clear. 未指定未初始化的atomic_flag对象是否具有set或clear的初始状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM