encyclopegia.org

Ticket lock

In computer science, a ticket lock is a synchronization mechanism, or locking algorithm, that is a type of spinlock that uses "tickets" to control which thread of execution is allowed to enter a critical section.[src]

In computer science, a ticket lock is a synchronization mechanism, or locking algorithm, that is a type of spinlock that uses "tickets" to control which thread of execution is allowed to enter a critical section.[src] ==Overview== The basic concept of a ticket lock is similar to the ticket queue management system.[src] This is the method that many bakeries and delis use to serve customers in the order that they arrive, without making them stand in a line.[src] Generally, there is some type of dispenser from which customers pull sequentially numbered tickets upon arrival.[src] The dispenser usually has a sign above or near it stating something like "Please take a number".[src] There is also typically a dynamic sign, usually digital, that displays the ticket number that is now being served.[src] Each time the next ticket number (customer) is ready to be served, the "Now Serving" sign is incremented and the number called out.[src] This allows all of the waiting customers to know how many people are still ahead of them in the queue or line.[src] Like this system, a ticket lock is a first in first out (FIFO) queue-based mechanism.[src] It adds the benefit of fairness of lock acquisition and works as follows; there are two integer values which begin at 0.[src] The first value is the queue ticket, the second is the dequeue ticket.[src] The queue ticket is the thread's position in the queue, and the dequeue ticket is the ticket, or queue position, that now has the lock (Now Serving).[src] When a thread arrives, it atomically obtains and then increments the queue ticket.[src] The atomicity of this operation is required to prevent two threads from simultaneously being able to obtain the same ticket number.[src] It then compares its ticket value, before the increment, with the dequeue ticket's value.[src] If they are the same, the thread is permitted to enter the critical section.[src] If they are not the same, then another thread must already be in the critical section and this thread must busy-wait or yield.[src] When a thread leaves the critical section controlled by the lock, it atomically increments the dequeue ticket.[src] This permits the next waiting thread, the one with the next sequential ticket number, to enter the critical section.[src] ==Fairness of lock acquisition== The notion of fairness in lock acquisition applies to the order in which threads acquire a lock successfully.[src] If some type of fairness is implemented, it prevents a thread from being starved out of execution for a long time due to inability to acquire a lock in favor of other threads.[src] With no fairness guarantees, a situation can arise where a thread (or multiple threads) can take a disproportionately long time to execute as compared to others.[src] A simple example will now be presented to show how a thread could be excessively delayed due to a lack of fairness in lock acquisition.[src] Assume a case where three threads, each executing on one of three processors, are executing the following pseudocode that uses a lock with no consideration for fairness.[src] while (true) { lock { // critical section } } Now further assume the physical arrangement of the three processors, P1, P2, and P3, results in a non-uniform memory access time to the location of the shared lock variable.[src] The order of increasing access time to the lock variable for the three processors is P1 TicketLock::acquire() would precede the critical section of the code and ticketLock_release would follow it.[src] Each processor will keep track of its turn via the value of each processor's myTicket .[src] Yan Solihin's pseudocode can be represented as in the following: import java.util.concurrent.atomic.AtomicInteger; class TicketLock { private AtomicInteger nowServing = 0; private AtomicInteger nextTicket = 0; public TicketLock() { this.nowServing = 0; this.nextTicket = 0; } public void acquire() { int myTicket = nextTicket.getAndIncrement(1); while (*nowServing != myTicket) { // Do nothing...[src] } } public void release() { nowServing.incrementAndGet(); } } Following along with the pseudocode above we can see that each time a processor tries to acquire a lock with TicketLock::acquire() , the fetch-and-add algorithm is called, returning the current value of nextTicket into the thread-private myTicket and incrementing the shared nextTicket .[src] The fetch and increment is done atomically, thereby not allowing any other concurrent attempts at access.[src] Once myTicket has been received, each thread will spin in the while loop while nowServing isn't equal to its myTicket .[src] Once nowServing becomes equal to a given thread's myTicket they are allowed to return from TicketLock::acquire() and enter the critical section of code.[src] After the critical section of the code, the thread performs TicketLock::release() which increments nowServing .[src] This allows the thread with the next sequential myTicket to exit from TicketLock::acquire() and enter the critical section.[src] Since the myTicket values are acquired in the order of thread arrival at the lock, subsequent acquisition of the lock is guaranteed to also be in this same order.[src] Thus, fairness of lock acquisition is ensured, enforcing a FIFO ordering.[src] The following table shows an example of ticket lock in action in a system with four processors (P1, P2, P3, P4) competing for access to the critical section.[src] Following along with the "Action" column, the outcome based on the above pseudocode can be observed.[src] For each row, the variable values shown are those after the indicated action(s) have completed.[src] The key point from the example is that the initial attempts by all four processors to acquire the lock results in only the first to arrive actually getting the lock.[src] All subsequent attempts, while the first still holds the lock, serves to form the queue of processors waiting their turn in the critical section.[src] This is followed by each getting the lock in turn, allowing the next in line to acquire it as the previous holder leaves.[src] Also note that another processor can arrive at any time during the sequence of lock acquire/releases by other processors, and simply waits its turn.[src] The first step, prior to use of the lock, is initialization of all lock variables (Row 1).[src] Having nextTicket and nowServing initialized to 0 ensures that the first thread that attempts to get the lock will get ticket 0, thus acquiring the lock due to its ticket matching nowServing .[src] So when P1 tries to acquire the lock it immediately succeeds and nextTicket is incremented to 1 (Row 2).[src] When P3 tries to acquire the lock it gets 1 for its myTicket , next ticket is incremented to 2, and it must wait since nowServing is still 0 (Row 3).[src] Next, when P2 attempts to acquire the lock it gets 2 for its myTicket , nextTicket is incremented to 3, and it must also wait due to nowServing still being 0 (Row 4).[src] P1 now releases the lock by incrementing nowServing to 1, thus allowing P3 to acquire it due its myTicket value of 1 (Row 5).[src] Now P3 releases the lock, incrementing nowServing to 2, allowing P2 to acquire it (Row 6).[src] While P2 has the lock, P4 attempts to acquire it, gets a myTicket value of 3, increments nextTicket to 4, and must wait since nowServing is still 2 (Row 7).[src] When P2 releases the lock, it increments nowServing to 3, allowing P4 to get it (Row 8).[src] Finally, P4 releases the lock, incrementing nowServing to 4 (Row 9).[src] No currently waiting threads have this ticket number, so the next thread to arrive will get 4 for its ticket and immediately acquire the lock.[src] ==Comparison of locks== The Linux kernel implementation can have lower latency than the simpler test-and-set or exchange based spinlock algorithms on modern machines.[src] Consider the table below when comparing various types of spin based locks.[src] The more basic locking mechanisms have lower uncontended latency than the advanced locking mechanisms.[src] ===Advantages=== One advantage of a ticket lock over other spinlock algorithms is that it is fair.[src] The waiting threads are processed in a first-in first-out basis as the dequeue ticket integer increases, thus preventing starvation.[src] A ticket lock algorithm also prevents the thundering herd problem occurring since only one thread at a time tries to enter the critical section.[src] Storage is not necessarily a problem as all threads spin on one variable, unlike array-based queueing locks (ABQL) who have threads spin on individual elements of an array.[src] ===Disadvantages=== One disadvantage is that there is a higher uncontended latency due to the extra instructions required to read and test the value that all threads are spinning on before a lock is released.[src] Another major disadvantage of a ticket lock is that it is non-scalable.[src] Research indicated that as processors are added to a Ticket Lock system, performance appears to decay exponentially.[src] Another problem comes from releasing a lock.[src] All threads are spinning on one variable, so when the lock is released there are Ө(p) invalidations (as well as Ө(p) acquisitions).[src] This is because all threads must reload their block into the cache and perform a test to determine their admittance to the critical section.[src] ==History== The ticket lock was introduced by Mellor-Crummey and Scott in 1991.[src] This algorithm was introduced into the Linux kernel in 2008 due to its advantages, but was omitted in paravirtualized environments where it had disadvantages.[src] , work is in progress to enable the use of ticket locks in paravirtualization.[src] As of March 2015 this type of locking scheme has been reemployed by Red Hat Enterprise Linux in their system.[src] ==Related work== Lamport's bakery algorithm uses a similar concept of a "ticket" or "counter" but does not make the use of atomic hardware operations.[src] It was designed for fault tolerance rather than performance.[src] Rather than all processors continuously examining the release counter, the bakery lock spins on examining the tickets of its peers.[src] Queue-based spin locks guarantee fairness by maintaining a queue of waiters and by granting the lock to the first waiter during an unlock operation.[src] ==See also== fetch-and-add, another atomic instruction that can be used in place of fetch-and-increment to implement a ticket lock ==References==[src]

77 sentences · 77 cited inline · 0 references stored

References

No reference rows published for this article.

Cite this article

MLA

"Ticket lock." encyclopegia.org, 2026, https://encyclopegia.org/wiki/ticket-lock. Accessed 27 November 2026.

Chicago

"Ticket lock." encyclopegia.org. Accessed November 27, 2026. https://encyclopegia.org/wiki/ticket-lock.

APA

encyclopegia.org. (2026). Ticket lock. Retrieved November 27, 2026, from https://encyclopegia.org/wiki/ticket-lock

Accessed November 27, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

lock

stub · references available

Lock and Dam No. 25

stub · references available

The Yellow Ticket

stub · references available

Hanham Lock

stub · references available

Another Ticket

stub · references available

gravity lock

stub · references available

lock

stub · references available

Lock of Herbede

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1371982072 · 0.5 · snapshot enwiki-001371982072

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools
  • Careers — open roles: frontier engineers and sales partners

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.