Race conditions occur when a program depends on the timing of other concurrent events such that an unexpected code path finishes before a expected code path expected to finish earlier.
Bank account example
You can consider a bank account (with $0
balance) as an example and you wanted to:
- A friend adds
$10
to the account - You make a payment of
$5
to a merchant
Assuming these operations occur simultaneously, there are two code paths here.
Code Path A
- The incoming funds is transferred from your friend
- The payment to the merchant is successful
Code Path B
- The payment to the merchant gets rejected
- The incoming funds is transferred from your friend
For more information, see the following reference note on Race Conditions.