Short answer: Peterson’s algorithm gives two threads mutual exclusion with three shared variables: a flag for each thread and a single turn. A thread raises its flag, gives the turn to the other thread, and waits while the other flag is up and the turn still belongs to the…
Category: Synchronization
How Lamport’s bakery algorithm takes a ticket
Short answer: Lamport’s bakery algorithm is an n-thread mutual-exclusion lock built from atomic reads and writes. A thread takes a ticket numbered one more than any ticket it can see, and it waits until every thread that holds a smaller ticket, or an equal ticket and a smaller id,…
