简体   繁体   English

标准::原子<bool>执行保证?</bool>

[英]std::atomic<bool> execution guarantee?

I know std::atomics are supposed to have well defined behavior, but I can't find an easy-to-digest online answer to this question: Do std::atomic.load() and.store() have execution guarantees?我知道 std::atomics 应该具有明确定义的行为,但我找不到这个问题的易于理解的在线答案:Do std::atomic.load() and.store() have execution guarantees?

If two threads attempt a concurrent write or read on the same std::atomic object, are both the write and read guaranteed to be executed?如果两个线程尝试对同一个 std::atomic object 进行并发写入或读取,是否保证写入和读取都被执行? In other words, is it possible either the write or read task will simply not get done?换句话说,是否有可能写入或读取任务根本无法完成? Will one or both be blocked?一个或两个会被阻止吗? Or are they guaranteed to be sequentialized?还是保证它们是按顺序排列的? I am NOT asking here about order of operations.不是在这里询问操作顺序。 I am asking simply if the operation will be done at some unspecified time in the future.我只是问手术是否会在未来某个未指定的时间完成。

It is a basic assumption that the compiler and processor ensures that the programmed operations are executed.编译器和处理器确保执行编程操作是一个基本假设。 This has nothing to do with std::atomic<> .这与std::atomic<>无关。 The guarantee which std::atomic<> offers is that single operations happen atomically. std::atomic<>提供的保证是单个操作以原子方式发生。

So what does that mean?那是什么意思呢?

Consider two threads, A and B, which both increment the same integer variable.考虑两个线程 A 和 B,它们都递增相同的 integer 变量。 This operation typically involves reading the integer, adding 1 and writing the result (notice that this is only an example, the C++ standard does not say anything about how operations are broken into atomic steps and thus we cannot make any assumptions about it based on the standard).此操作通常涉及读取 integer、加 1 并写入结果(请注意,这只是一个示例,C++ 标准并未说明如何将操作分解为原子步骤,因此我们无法根据标准)。

In this case we would have the steps "read", "add one" and "write".在这种情况下,我们将有“读取”、“添加一个”和“写入”步骤。 For each thread, these steps are guaranteed to the executed in that order, but there is no guarantee how the steps are interleaved.对于每个线程,这些步骤都保证按顺序执行,但不能保证这些步骤是如何交错的。 It may be:它可能是:

   B: read
   B: add1
   B: write
   A: read
   A: add1
   A: write

which results in the integer being incremented twice.这导致 integer 递增两次。

It could also be也可以是

   A: read
   A: add1
   B: read
   B: add1
   B: write
   A: write

which would result in the integer being incremented only once.这将导致 integer 仅递增一次。

Thus this implementation would have a race condition .因此这个实现会有一个竞争条件

To get rid of that we can use a std::atomic<int> instead of a plain int for the integer. std::atomic<int> implements the ++ operator and the guarantee which std::atomic<> provides is that incrementing in this case will happen atomically.为了摆脱它,我们可以使用std::atomic<int>而不是 integer 的普通 int。std::atomic< int std::atomic<int>实现++运算符,而std::atomic<>提供的保证是在这种情况下递增将自动发生。

In the example, this means that the sequence of steps - read, add one, write - will not be interrupted by another thread.在示例中,这意味着步骤序列(读取、添加一个、写入)不会被另一个线程中断。 There is still no guarantee about the order of execution between the threads.仍然无法保证线程之间的执行顺序。 Hence, we could have因此,我们可以有

   A: read
   A: add1
   A: write
   B: read
   B: add1
   B: write

or或者

   B: read
   B: add1
   B: write
   A: read
   A: add1
   A: write

but other combinations will not be possible and in both cases the integer will be incremented twice.但其他组合是不可能的,在这两种情况下,integer 将递增两次。 Thus there is no race condition.因此不存在竞争条件。

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

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