我想知道是否可以使用单个原子操作执行原子变量的比较和递增。这是我到目前为止所写的(线程的片段代码)
std::atomic<int> counter; //global variable
if(counter<25)
{
counter++;
}
else
{
//send serial/socket data
}
我知道我做错了,因为原子变量计数器被访问了两次(一次用于获取数据,另一次用于递增)。但是如果另一个线程在获取变量值之后和递增之前对'counter'执行一些更新操作,这可能会导致问题。所以我想知道是否可以一次完成这两个操作。我也不想使用互斥体。
最佳答案
在if(counter<25) counter++;
在读取计数器和更新它之间存在竞争条件(即原子加载后跟原子加载-修改-存储)。
它需要一个比较交换循环来确保读取的值自那以后没有改变。如果已更改,则需要重试操作。
类似下面的内容:
std::atomic<int> counter;
auto value = counter.load(std::memory_order_relaxed);
while(value < 25) {
if(counter.compare_exchange_weak(value, value + 1, std::memory_order_release, std::memory_order_relaxed))
break; // Succeeded incrementing counter.
// compare_exchange_weak failed because counter has changed.
// compare_exchange_weak reloaded value with the new value of counter.
// Retry.
}
if(!(value < 25))
// Failed to increment because counter is not less than 25.
关于multithreading - 如何比较和递增原子变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47753528/