while (true) { // Busy-wait loop: Wait if buffer is full while (counter == SIZE); // Insert nextItem into the buffer at position 'in' buf[in] = nextItem; // next position in = (in + 1) % SIZE; counter++;}
Reader process Q: Consumer
while (true) { // Busy-wait loop: Wait if the buffer is empty while (counter == SIZE); // Read nextItem = buf[out]; // next position out = (out+1) % SIZE; counter --;}
Where counter is shared variable
Implementation of counter ++
register1 = counter
register1 = register1 + 1
counter = register1
Implementation of counter—
register2 = counter
register2 = register2 - 1
counter = register2
Process P and Q can get different counter values at the same time if line 3 of both implementations are executing