我正在使用无锁堆栈(通过标记指针)来管理小内存块池。当块被插入到池中和从池中移除时,列表节点就地创建和销毁。
这是一个非常简化的测试程序,它只从堆栈中弹出。因此,没有 ABA 问题,也没有标记指针。足以证明我遇到的比赛:
#include <atomic>
#include <list>
#include <thread>
#include <type_traits>
struct Node {
Node() = default;
Node(Node *n) { next.store(n); }
std::atomic<Node *> next;
};
using Memory = std::aligned_storage_t<sizeof(Node)>;
struct Stack {
bool pop_and_use() {
for (Node *current_head = head.load(); current_head;) {
Node *next = current_head->next.load(); // READ RACE
if (head.compare_exchange_weak(current_head, next, std::memory_order_seq_cst)) {
current_head->~Node();
Memory *mem = reinterpret_cast<Memory *>(current_head);
new (mem) int{0}; // use memory with non-atomic write (WRITE RACE)
return true;
}
}
return false;
} …Run Code Online (Sandbox Code Playgroud)