Process A | Process B | concurrent access |
---|---|---|
X = 1; | Y = 2; | does not matter |
X = Y + 1; | Y = Y * 2; | important! |
while (true) { /* produce an item in next Produced */ while (counter == BUFFER_SIZE) ; /* do nothing */ buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; counter++; }
while (true) { while (counter == 0) ; /* do nothing */ nextConsumed = buffer [out] ; out = (out + 1) % BUFFER_SIZE; counter--j; /* consume the item in nextConsumed */ }