In the preceding recipes, we learned how to synchronize access of multiple threads to shared data, using mutexes and locks. If several threads try to run critical sections of the code protected by a lock, only one thread at a time can do it. All other threads have to wait until that thread leaves the critical section.
In some cases, however, it is possible to synchronize access to shared data without mutexes and explicit locks. The idea is to use a local copy of data for modification, and then update the shared copy in a single, uninterruptible, and undividable operation.
This type of synchronization depends on the hardware. Target processors should provide some form of Compare And Swap (CAS) instruction. This checks whether the value in a memory location matches a given value, and replaces it with a new given value only if they match. Since...