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 and resource are used by thread and thread
- starts to use
- and try to start using
- wins and gets B first
- Now needs to use
- is locked by , which is waiting for
-
Illustrating Race Conditions with ATM Transactions
- Let's say a husband and wife have a debit card for the same checking account
- They have dollars in this bank account
- Suppose they make different transactions at the same time
- The wife purchases a meal for dollars
- The husband purchases a set of pots for dollars
-
Specifically, this scenario involves the following:
T1:
T2:
Bank Account:
-
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 | ||
---|---|---|
Read account: | ||
Read account: | ||
Write new amt: | ||
Write new amt: | ||
end | ||
end |
- After both transactions, the account has dollars
- Without locking, this is 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 | ||
---|---|---|
Lock() | ||
Lock() | ||
Write | ||
Write | ||
Lock(y) | ||
Write | ||
Lock(x) | ||
Write | ||
Unlock(x) | ||
Unlock(x) | ||
Unlock(y) | ||
Unlock(y) |
- Specifically, line contains a deadlock
- and attempt to grab each other's resources and
- 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 1Criminal:
Thread 2Hostage:
Resource 1Criminal'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