= Test-and-set
{wiki}
An <atomic read-modify-write> that returns a flag's old value while setting it. In a <sequential consistency> model with the flag initially $0$, `exchange(flag, 1)` permits entry only when it returns $0$; an owner resets the flag to $0$ on release. Between releases only one exchange can observe $0$. By contrast, separate `read(flag)` and `write(flag, 1)` steps allow both contenders to read $0$ before either writes $1$. The indivisibility of the exchange is an assumed primitive, not something proved by writing its steps as ordinary assignments.<Operating Systems: Three Easy Pieces by Remzi H. Arpaci-Dusseau and others/Locks>{ref}
Back to article page