Jump to content

Lock (computer science)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Clausen (talk | contribs) at 12:43, 12 June 2003. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

In computer science, a lock is a mechanism for serializing access to a piece of data in an environment where there are many threads of execution. Each thread cooperates by acquiring the lock before accessing the corresponding data.

A semaphore is the simplest type of lock. No distinction is made between shared (read only) or exclusive (read and write) modes. Other schemes provide for a shared mode, where several threads can acquire a shared lock for read-only access to the data. Other modes such as shared, exclusive, intend-to-exclude and intend-to-upgrade are also widely implemented.

A spinlock is a lock where the thread simply waits ("spins") until the lock becomes available. It is very efficient if other threads are only likely to require access to the data for a short period of time. It is wasteful if the lock is held for a long period of time.

Locks require hardware support for safe implementation. This is usually provided by an atomic "compare-and-swap" instruction.

See also: