Race Conditions
Home ] Up ] A Steam Boiler ] [ Race Conditions ] Monitors ] Synchronization ] A Fixed Steam Boiler ] Deadlock ] Starvation & Livelock ]

 

 

The problem is a classical problem in multi-threaded code -- a race condition. What you probably expected to happen was:

  1. Thread A reads the pressure gauge
  2. Thread A updates the pressure gauge
  3. Thread B reads the pressure gauge
  4. Thread B updated the pressure gauge

However, what actually happens is more like:

  1. Thread A reads the pressure gauge
  2. Thread B reads the pressure gauge
  3. Thread A updates the pressure gauge
  4. Thread B updates the pressure gauge

In other words, Thread A was preempted by Thread B, and then back again, and then again. The result is that Thread A's update is overwritten by Thread B, and the check that you thought was happening is made useless.

When you have multiple concurrent activities updating a shared data item, you have to be more careful -- you have to synchronize access to that data item.

 

The page was last updated February 19, 2008