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 question is, are operations in the form of B[X]
, where B is an array and X is an integer atomic ? In my example for instance, is it possible that after a fetch_add
call for the push()
method is executed, and before the B[X]
is executed, that a whole pop
and push
in separate threads could be executed, causing a push
to overwrite another push
?
Source: Windows Questions C++