As both the increment and decrement operations are atomic the given solution works correctly. This is because the only time both Producer and Consumer access the same memory region is when the buffer is either full or empty (when in = out). But when buffer is empty, only producer can proceed and when buffer is full, only consumer can proceed.