Simpsons 4-Slot Algorithm
Simpsons 4-Slot Algorithm is an algorithm for controlling access to a shared variable that does not need any locks or Mutexes.
Idea is to duplicate the shared state 4 times into a
array d, and to use 4 atomic control bits to
synchronize access:
The bits
li, riindicate the last written location in each pair.The bit
rpstores the pair that the reader is reading from.The bit
ppoints to the pair the writer last wrote toWriter writes data into a pair of slots in an alternating fashion
Reader concurrently tries to read the last written item
To avoid a Race Condition where you are simultaneously reading and writing, add an extra pair of slots that you atomically swap when you go to read.
This algorithm does not have any critical section.