<< Previous exercise (3.45)
| Index |
Next exercise (3.47) >>
Suppose we have a mutex with cell (list false), and both Peter and Paul attempt to acquire it. The following may happen if test-and-set! has *not* been made atomic using without-interrupts:
1. Peter calls test-and-set!, (car cell) is false.
2. Paul *also* calls test-and-set!, *also* finds (car cell) to be false, since Peter's test-and-set! has not completed yet.
3. Peter sets (car cell) to true.
4. Paul *also* sets (car cell) to true.
Here, both Peter and Paul have acquired the mutex, which is bad.
As discussed in the book, making test-and-set! atomic using without-interrupts would require step 3 to happen before step 2, which would force Paul to wait for Peter to release the mutex before being able to acquire it.