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.
Japanese name: データ競合
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.
Japanese name: データ競合
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.
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.
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.
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.
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.
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.
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.
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.
Data race appears in 4 practice questions across 1 topic, including Multicore & Synchronization.
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.