IT lexicon › Programming › Test-and-set

Test-and-set

Programming På svenska → Updated: 2026-11-16

An atomic machine instruction that reads a value, tests it against an expected value, and writes a new value in a single, indivisible step.

Test-and-set is one of the simplest atomic primitives for building higher-level synchronization mechanisms. The instruction reads a value at a memory address, compares it with an expected value, and writes a new value only if the comparison matches—all without allowing other threads to read or write that location in between. This enables lock implementations without requiring external semaphores or kernel support.

Most processors provide variants like CAS (compare-and-swap) or other atomic instructions with specialized semantics. Test-and-set predates modern formal memory models and is typically anchored directly in hardware rather than expressed through memory-model guarantees.

← Back to the lexicon