Home

datarace

A datarace, or data race, is a condition in concurrent programming where two or more threads access the same memory location concurrently, and at least one of the accesses is a write, with no proper synchronization or ordering guarantees between the accesses. When a datarace can occur, the program’s behavior becomes unpredictable and often undefined depending on the language and memory model.

Dataraces arise from shared mutable state that is accessed without appropriate synchronization. Causes include omissions of

The formal treatment of dataraces varies by language. In C and C++11 and later, a data race

Detection and mitigation efforts include using race detectors (such as ThreadSanitizer or Helgrind), static analysis, and

mutexes
or
locks,
using
non-thread-safe
data
structures,
improper
use
of
atomic
operations,
or
complex
interactions
between
threads
where
one
thread
updates
data
while
another
reads
or
writes
it
at
the
same
time.
The
consequences
can
include
corrupted
data,
lost
updates,
stale
reads,
crashes,
or
security
vulnerabilities,
and
problems
may
be
intermittent
and
hard
to
reproduce.
is
defined
as
concurrent
conflicting
accesses
to
the
same
atomic
non-atomic
object
without
a
happens-before
relationship,
yielding
undefined
behavior.
In
Java,
a
data
race
occurs
when
unsynchronized
reads
and
writes
to
shared
variables
happen
without
a
happens-before
ordering,
potentially
leading
to
visibility
and
ordering
issues;
the
Java
Memory
Model
provides
mechanisms
(synchronized
blocks,
volatile
fields,
and
concurrent
data
structures)
to
prevent
races.
careful
design
practices.
Mitigations
focus
on
proper
synchronization
(mutexes,
locks,
atomic
operations),
using
thread-safe
or
immutable
data,
reducing
shared
state,
and
avoiding
data-dependent
race
conditions.