I have two threads which share a circular queue. The contents of the queue are unsigned numbers (unsigned long
on x86_64). One thread is the producer and the other consumer. The producer only writes to an element of the queue if the value of the element in the queue is 0 and producer always produce a non-zero value, whereas consumer only consumes it when its value is non-zero. Also consumer resets the element to 0 whenever it consumes it, so that producer get to know that consumer has consumed it.
Now what I think is that since with this scheme, there is strict access order of elements in the queue, we don't require using synchronization or atomic variables. Is my assumption correct? Or I'm a missing something here? Keep in mind that x86_64 has a relatively strict consistency memory model and only unrelated loads can be placed before a store. Also it has cache coherency which pro-actively updates the caches. Also I use volatile
variables to be sure that compilers don't cache them.