I've compiled and analyzed the assembly output for:
struct S{
public:
int a,b,c,d,e,f,g,h,i,j,k;
};
int main() {
S s;
std::atomic<S> as;
as.store(s);
return 0;
}
I want to see how does is it implemented atomic store
in fact. It is easy when it comes to aligned "small" operands. But, now we have a wider operand so it is a more complicated situation.
In my other question ( Atomicity on x86) @Peter Cordes said:
For wider operands, like atomically writing new data into multiple entries of a struct, you need to protect it with a lock which all accesses to it respect. (You may be able to use x86 lock cmpxchg16b with a retry loop to do an atomic 16b store. Note that there's no way to emulate it without a lock.)
Ok, but what does it mean exactly? What does it mean to lock?
Especially, I know that lock
is a prefix that ensures about atomicity of "prefixed" instruction. Especially, @Peter Cordes said:
You may be able to use x86 lock cmpxchg16b with a retry loop to do an atomic 16b store
I cannot understand how it is possible to keep it atomic? Ok, I can imagine that 16B chunk of memory can be stored in atomic way? But what about next iterations?
I hope that my doubts are understandable because I had a problem to express it.
I was debugging above program and, on my eye, the magic is behind atomic_store
.
I suppose that this function executes what @Peter Cordes said. If someone wants, I can paste here disassembled __atomic_store