Friday, July 6, 2012

Race Condition vs Deadlock

Race Condition & Deadlock are not the same. Please refer following examples to understand the differences.
 
Race Condition: When two threads randomly tries to update a common variable.
 
Eg: 

  1. Var1 = 100
  2. Thread T1 & Thread T2 reads Var1 at same time
  3. But, T2 updates Var1 to 120 & exits
  4. Then, T1 updates Var1 to 150 & exits
  5. This leads to confused result for Var1
[Expected result is 100 + 20 + 50 = 170]

 
Dead Lock: Two thread waiting without end
 

Eg:
  • Var1 is locked & used by Thread T1.
  • Var2 is locked & used by Thread T2.
  • T1 waits for Var1 to be released & T2 waits for Var2 to be released leading to dead lock.
http://support.microsoft.com/kb/317723

No comments:

Post a Comment