Description:

  • Nondeterministic behavior caused by the times at which two or more threads access a shared variable
  • Debug race condition is hard
  • example: Bounded-buffer Problem, Producer and Consumer Problem
    • Writer Process P, Producer
      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