我担心嵌套指针和访问,特别是在处理基于无锁节点的树结构时是否有办法避免这个 ABA 问题。
我的担忧如下:
标准是否对此做出保证并且 funcB 是否等同于 funcA?
如果这里存在 ABA 问题,是否有解决无锁编程嵌套成员访问的方法?
#include <atomic>
#include <iostream>
struct Foo
{
std::atomic_int value;
};
struct Bar
{
Foo * foo;
};
void funcB(Bar * bar)
{
if (not bar->foo->value.fetch_or(1) )
{
//Something
}
}
void funcA(std::atomic_int * bar)
{
if (not bar->fetch_or(0))
{
//Something
}
}
上面的汇编输出是:
funcB(Bar*): # @funcB(Bar*)
mov rax, qword ptr [rdi]
lock or dword ptr [rax], 1
ret
funcA(Foo*): # @funcA(Foo*)
lock or dword ptr [rdi], 1
ret