Category : atomicity

So I wanted to try implementing a fixed sized, wait free stack with the fetch_add and fetch_sub atomic instructions. Say that I have a basic stack, with two operations, push and pop. struct WaitFreeStack { std::atomic<size_t> cur; std::atomic<int>* buf; void push(int i) { buf[cur.fetch_add(1)].store(i); } int pop() { return buf[cur.fetch_sub(1) – 1].load(); } }; My ..

Read more

Clang doesn’t inline std::atomic::load for loading 64-bit structs Consider the following code, which uses a std::atomic to atomically load a 64-bit object. #include <atomic> struct A { int32_t x, y; }; A f(std::atomic<A>& a) { return a.load(std::memory_order_relaxed); } With GCC, good things happen, and the following code is generated. (https://godbolt.org/z/zS53ZF) f(std::atomic<A>&): mov rax, QWORD PTR ..

Read more