The algorithm is modeled on a waiting room with an entry and exit doorway.5 Initially the entry door is open and the exit door is closed. All processes which request entry into the critical section at roughly the same time enter the waiting room; the last of them closes the entry door and opens the exit door. The processes then enter the critical section one by one (or in larger groups if the critical section permits this). The last process to leave the critical section closes the exit door and reopens the entry door, so the next batch of processes may enter.
The implementation consists of each process having a flag variable which is written by that process and read by all others (this single-writer property is desirable for efficient cache usage).
The flag variable assumes one of the following five values/states:
The status of the entry door is computed by reading the flags of all N processes. Pseudo-code is given below:
Note that the order of the "all" and "any" tests must be uniform. Also the "any" tests should be satisfied by a thread other than self. For example, if the test is any flag[1..N] = 1 and only flag[self] = 1, then the test is said to have failed/returned 0. Despite the intuitive explanation, the algorithm was not easy to prove correct, however due to its favorable properties a proof of correctness was desirable and multiple proofs have been presented.67
Szymański, Bolesław K. (1988). "A simple solution to Lamport's concurrent programming problem with linear wait". Proceedings of the 2nd international conference on Supercomputing - ICS '88. St. Malo, France: ACM. pp. 621–626. doi:10.1145/55364.55425. ISBN 978-0-89791-272-3. S2CID 18612278. 978-0-89791-272-3 ↩
Manna, Zohar; Pnueli, Amir (1990). "An Exercise in Verification of Multi-Process Programs.". Beauty is Our Business: A Birthday Salute to Edsger W. Dijkstra. Springer Verlag. pp. 289–301. ISBN 978-0-387-97299-2. 978-0-387-97299-2 ↩
Szymański, Bolesław (1990). "Mutual Exclusion Revisited" (PDF). Fifth Jerusalem Conference on Information Technology. Jerusalem, Israel: 110–117. http://cs.rpi.edu/~szymansk/papers/jerus.93.pdf ↩
Lamport, Leslie (1986). "The Mutual Exclusion Problem - Part II: Statement and Solutions". Journal of the ACM. 33 (2): 327–348. CiteSeerX 10.1.1.32.9808. doi:10.1145/5383.5385. S2CID 7387839. /wiki/CiteSeerX_(identifier) ↩
de Roever, Willem-Paul; de Boer, Frank; Hannemann, Ulrich; Hooman, Jozef; Lakhnech, Yassine; Poel, Mannes; Zwiers, Job (November 2001). Concurrency Verification. Number 54 in Cambridge Tracts in Theoretical Computer Science. Cambridge University Press. ISBN 978-0-521-80608-4. 978-0-521-80608-4 ↩