有没有办法有一个版本的std::atomic的compare_exchange_strong方法,交换不平等?



我有一个原子类型,需要将其与一个值进行原子比较,如果这两个值不相等,则交换原子的值。

换句话说,compare_exchange_strong本质上是原子地执行此操作:

if (atomic_value == expected)
atomic_value = desired;

我正在寻找一种方法来做到这一点:

if (atomic_value != expected)
atomic_value = desired;

(是的,我知道compare_exchange_strong使用按位相等进行比较,而不是==运算符。我知道当比较失败时会分配expected的值。这只是为了说明。在我的用例中,无论比较结果如何,我都不需要原子的值。(

有没有什么方法可以做到这一点,而不必重新使用锁而不是std::atomic

auto observed = atomic_value.load();
for (;;)
{
if (observed == expected){
break; // no exchange
}
if (atomic_value.compare_exchange_weak(observed, desired)) { 
break; // successfully exchanged observed with desired
}
}

当然,它在HW具有LL/SC的体系结构上是次优的,因为C++不公开它。使用LL/SC可以有任意条件。

您可以使用这样的东西:

#include <atomic>
#include <random>
std::atomic<int> atomVal;
int store_if_not_equal(int desired)
{
while (true)    // or maxloop....
{
int expected = desired;
if (atomVal.compare_exchange_strong(expected, desired))
{
// values matched - do nothing
return 0;
}
else
{
//expected now contains the "current value"
// another thread could have sneaked in and changed it,
// so only replace it if it still matches
if (atomVal.compare_exchange_strong(expected, desired))
{
// success
return 1;
}
}
// if we arrive here, retry
}
}
int main()
{
atomVal.store(rand());
return store_if_not_equal(2);
}

演示:https://godbolt.org/z/qWTP7canf

只需使用通常用于比较和交换的循环,但不要循环直到(新的(期望值匹配,而是循环,直到它匹配(并且存储发生(或等于!= expected条件中的值,因为在这种情况下,您不必做任何事情。(显然,不要让初始值是"除非"值,这样你就不能第一次"成功"。(

最新更新