posix standard says that things like mutex will enforce a memory sync. However, the compiler may reorder the memory access. Say we have
lock(mutex);
setdata(0);
ready = 1;
unlock(mutex);
It might be changed to code below by compiler reordering, right?
ready = 1;
lock(mutex);
setdata(0);
unlock(mutex);
So how can mutex sync the memory access? To be more precise, how do compilers know that reordering should not happen across lock/unlock?
actually here for single thread aspect, ready assignment reorder is totally safe since ready is not used in function call lock(mutex).
EDITED: So if function call is something that compiler will not get across, can we regard it as a compiler memory barrier like
asm volatile("" ::: "memory")
lock()
andunlock()
are simply function calls for the compiler, I doubt that such a reordering will happen