What is Data race?

A situation in which threads access the same variable concurrently without synchronization and at least one access is a write. Results vary between runs; in C and C++ it is undefined behavior.

Field: Multicore & Synchronization

Japanese name: データ競合

How a lost update happens

The statement counter++ looks atomic but compiles to three steps: load the value, add 1, store it back. Suppose two threads each increment a shared counter 100,000 times. If thread A loads 100 and thread B also loads 100 before A stores, both store 101 and one increment disappears. Depending on how the threads interleave, the final value can be anywhere up to 200,000 (usually below it) and differs from run to run. Nothing crashes, so the bug is easy to miss, and it may vanish when the program is slowed down by a debugger or logging.

Data race vs. race condition

A data race is precisely defined: two threads access the same memory location concurrently, at least one access is a write, and no synchronization orders them. A race condition is broader: any bug whose outcome depends on timing. The two overlap but are not identical. A check-then-act sequence built from individually atomic operations has no data race yet can still be a race condition, because another thread can act between the check and the act. Languages treat data races differently: in C and C++ a program containing one has undefined behavior, while Java gives racy programs defined but weak semantics.

Fixing it: locks and atomics

The cure is to impose an order on the conflicting accesses. A lock (mutex) turns the update into a critical section that only one thread executes at a time; keeping critical sections short limits waiting. For simple operations, atomic instructions are faster: fetch-and-add, such as x86 lock xadd or RISC-V amoadd.w, performs the read, add and write indivisibly and returns the old value, so it never needs to retry. More complex updates use compare-and-swap loops. Declaring a variable volatile in C or C++ does not prevent a data race, because it provides neither atomicity nor ordering between threads.

Why compilers and hardware make races worse

Because C and C++ assume race-free programs, compilers may legally keep a shared variable in a register, hoist a load out of a loop or merge stores, so a thread spinning on a flag written by another thread without synchronization might never see the change. Hardware adds its own reordering through store buffers and caches. Synchronization operations, such as lock acquisition and atomics with the appropriate memory ordering, are what tell both compiler and hardware where ordering must be preserved. Dynamic race detectors such as ThreadSanitizer help find races that testing alone rarely exposes.

Try it: questions about Data race

Which describes a data race?

  1. AMultiple threads only read the same variable at the same time
  2. BOne thread writes the same variable twice in a row
  3. CMultiple threads access the same variable at the same time without synchronization, and at least one access is a write
Show answer and explanation

Answer: C. Multiple threads access the same variable at the same time without synchronization, and at least one access is a write

A data race occurs when the order of reads and writes to one location is not fixed by synchronization, so the result depends on the order in which they happened to run. Because it appears or not depending on timing, it is a hard bug to find by testing. In C and C++, a program with a data race has undefined behavior. Locks or atomic variables must be used to establish an order.

FAQ

What is the difference between a data race and a race condition?

A data race is concurrent, unsynchronized access to the same memory location where at least one access is a write. A race condition is any timing-dependent bug, which can exist even when every individual access is properly synchronized.

Can a data race happen on a single-core CPU?

Yes. If the operating system preempts a thread between its load and store, another thread can run and update the same variable, so an increment can still be lost without any true parallelism.

Does the volatile keyword prevent data races in C and C++?

No. Volatile only forces the compiler to perform each access; it does not make read-modify-write sequences atomic or order them between threads. Use atomics or locks instead.

Questions that mention Data race (3)

Data race appears in 4 practice questions across 1 topic, including Multicore & Synchronization.

  1. When two threads run the code above simultaneously, the final counter can be less than 200000. Why?
  2. What does a fetch-and-add instruction, such as RISC-V amoadd.w or x86 lock xadd, do?
  3. Which describes a critical section?

Books on Multicore & Synchronization Ad

As an Amazon Associate I earn from qualifying purchases.

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary