Home

spinwaits

Spinwaits are a synchronization technique in which a thread waiting for a condition to become true repeatedly tests the condition in a tight loop, rather than sleeping or blocking. They are closely related to busy-waiting and are often used in low-level code that requires very low latency or where the expected wait time is short.

In practice, a spinwait loops while the condition is not satisfied, optionally performing a pause or backoff

Spinwaits are commonly used to wait for lightweight events such as a flag becoming visible, a lock

In practice, spinwaits are often complemented with blocking synchronization as a fallback: if the wait exceeds

in
each
iteration
to
reduce
resource
contention.
On
modern
CPUs,
instructions
such
as
a
processor
pause
or
a
hint
to
the
scheduler
help
the
core
avoid
pipeline
stalls
and
reduce
power
usage
while
spinning.
Some
implementations
include
short
sleeps
or
exponential
backoff,
especially
when
waits
may
be
longer
than
a
few
dozen
cycles.
becoming
available,
or
a
memory
location
changing
under
lock-free
data
structures.
They
are
typically
preferred
when
blocking
would
be
more
expensive
than
spinning
and
when
the
wait
time
is
expected
to
be
brief.
However,
spinning
consumes
CPU
time,
can
waste
cycles
on
multi-core
systems,
and
can
cause
priority
inversion
or
starvation
if
misused.
a
small
threshold,
the
thread
yields
the
processor
or
blocks
on
a
traditional
primitive
such
as
a
condition
variable
or
futex.
Developers
must
consider
the
platform’s
scheduling
behavior,
cache
coherence,
and
memory
ordering
guarantees
when
implementing
spinwaits.