Jump to content

Lamport's Bakery algorithm

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Grahar64 (talk | contribs) at 04:18, 13 June 2006. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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