文章中有一段代码
int c;
if ((c = cmpxchg (val, 0, 1)) != 0) {
//something
}
// cmpxchg(var, old, new): The content of the variable var will be replaced with
// new if the current value is old. Regardless, the current value of var before the
// operation is returned.
std 中没有这样的 CAS 指令实现...
我试图模拟这段代码:
std::uint32_t с = 0;
if ((с = std::atomic_compare_exchange_strong(&val, &c, 1) ? 0 : 1) != 0) {
//something
}
这有多正确?
编辑:待回答
std::uint32_t с = 0;
if (!std::atomic_compare_exchange_strong(&val, &c, 1)) {
//something
}