Lamport's Bakery algorithm
Lamport's Bakery algorithm is used to define critical sections in concurrent code.
Basically there is:
a Global array (element in each thread) of tickets
a Global array to say weather choosing or not
And with these arrays the thread does:
Say choosing and pick next available number
Say have stoped choosing
Loop compares number with other threads
Enter the critical section if no lower number is left
This algorithm works(except 2 threads may get the same ticket simultaneously, then pick at random)
This is also a very ineffiecient algorithm for dealing with concurrency, because there is alot of busy waiting, this is when a thread that is not doing anything towards a goal, is taking up CPU time.
There are many better ways of concurrency including monitors or semaphores