Race Conditions and Deadlocks

Defining Intuition of Locks

  • A Lock is an object that acts like a hall pass
  • Only one thread at a time can have the Lock
  • Any other thread that wants the Lock must wait until the owner of the Lock gives it up
  • Specifically, a lock allows only one thread to enter locked code
  • Only one thread can acquire the lock at a time

Motivating the Use of Locks

  • Splitting a process into multiple threads can create issues
  • Specifically, we can experience race conditions
  • A race condition occurs when:

    • Two or more threads access shared data
    • They try to change it at the same time
  • To prevent race conditions, we can introduce locking
  • However, incorrect locking can lead to deadlocks

Defining Problems related to Locks

  • A deadlock occurs when:

    • Two or more threads are waiting on a resource being used
    • As a result, they are blocking each other
  • For example:

    • Resource AA and resource BB are used by thread XX and thread YY

      1. XX starts to use AA
      2. XX and YY try to start using BB
      3. YY wins and gets B first
      4. Now YY needs to use AA
      5. AA is locked by XX, which is waiting for YY

Illustrating Race Conditions with ATM Transactions

  • Let's say a husband and wife have a debit card for the same checking account
  • They have 100100 dollars in this bank account
  • Suppose they make 22 different transactions at the same time
  • The wife purchases a meal for 1010 dollars
  • The husband purchases a set of pots for 5050 dollars
  • Specifically, this scenario involves the following:

    • T1: $10-\text{\textdollar}10
    • T2: $50-\text{\textdollar}50
    • Bank Account: $100\text{\textdollar}100
  • In this scenario, the bank must take:

    • Receive a transaction amount
    • Read what is currently in their account
    • Then modify the amount
  • Since these transactions happen at the same time, the transactions could look like the following:
Step in Time T1T_{1} T2T_{2}
11 Read account: $100\text{\textdollar}100
22 Read account: $100\text{\textdollar}100
33 Write new amt: $90\text{\textdollar}90
44 Write new amt: $50\text{\textdollar}50
55 end
66 end
  • After both transactions, the account has 5050 dollars
  • Without locking, this is 1010 dollars more than it should be
  • This scenario is a race condition

Illustrating Deadlocks with ATM Transactions

  • The transactions between the husband and wife are expected to be serializable
  • Meaning, the balance would be the same if the transactions had occurred different times
  • Therefore, we need to introduce locks
  • However, introducing locks can introduce deadlocks
  • A deadlock can occur in the following scenario:
Step in Time T1T_{1} T2T_{2}
11 Lock(xx)
22 Lock(yy)
33 Write x=1x=1
44 Write y=19y=19
55 Lock(y)
66 Write y=x+1y=x+1
77 Lock(x)
88 Write x=y+2x=y+2
99 Unlock(x)
1010 Unlock(x)
1111 Unlock(y)
1212 Unlock(y)
  • Specifically, line 55 contains a deadlock
  • T1T_{1} and T2T_{2} attempt to grab each other's resources yy and xx
  • However, they can't acquire them because they are locked
  • Meaning, they will continue waiting forever for each other's resource

Illustrating Deadlocks from Old Crime Scenes

  • Imagine a criminal and a cop are at a standoff with each other
  • The criminal holds a hostage, while the cop holds the criminal's friend
  • The criminal and cop will only release their held person if the other releases their person first
  • Specifically, the cop will only release the criminal's friend if the criminal releases the hostage first
  • Similarly, the criminal will only release the hostage if the cop releases the criminal's friend first
  • Therefore, they are at a deadlock:
  • In this analogy, the characters represent:

    • Cop: Thread 1
    • Criminal: Thread 2
    • Hostage: Resource 1
    • Criminal's Friend: Resource 2
  • In this analogy, the following can be translated:

    • Thread 1 demands resource 2, but thread 2 owns the lock
    • Thread 2 demands resource 1, but thread 1 owns the lock
    • The owner of resource 2 is thread 1
    • The owner of resource 1 is thread 2

References

Previous
Next

threading.Timer

threading.Lock