Systems
Lamport's mutual exclusion algorithm, one memory operation at a time.
C++ implementation; the demo simulates the canonical algorithm with a step-level scheduler.
C++pthreadsstd::atomicTypeScript (demo)

Overview
Lamport's Bakery algorithm for N threads using pthreads and atomics. Threads take a ticket one larger than any they can see, then wait for every thread with a smaller (ticket, id) pair.
Interactive demo
Each thread is a state machine that yields after every shared read or write, so you can watch tickets being taken and compared. Turn off the choosing flags to see mutual exclusion break.
Limitations
- The C++ version deviates from the canonical algorithm: it re-takes tickets inside the wait loop, increments the ticket on unlock instead of clearing it, and uses relaxed memory ordering.
- Built and run, the original program prints the first thread entering the critical section and then hangs, a consequence of the unlock step. The simulator implements the canonical form instead.