 | | In a race condition, two or more threads access the same resource at the same time. In Figure 3-1, Thread 1 and Thread 2 simultaneously attempt a withdrawal from the same bank account. Thread1 reads the current balance of the account—$125. However, before it can proceed with the other steps that complete the withdrawal (the arithmetic, the storing of the new result, and the dispensing of cash), it is preempted by Thread 2. Thread 2 also reads the account balance—$125, but Thread 2 continues on to complete the transaction. It subtracts $50 from$125, stores the new balance, $75, in the account database, and hands the customer a $50 bill. Sometime thereafter, Thread 1 resumes, subtracts $50from $125 (which is what it thinks is the account balance), stores the new balance, $75, in the account database, and hands the other customer a$50 bill. Nothing looks wrong to either thread, but in actuality, we've allowed one thread to clobber the write of another. We've subtracted a total of $100 from $125 and have come up with $75. A bank could lose a lot of money if this was allowed to happen.* | |
|