liveBook · Manning

Data race

As explained in The Coder Cafe

A data race is a concurrent execution in which multiple threads access the same memory location, with at least one access being a write, without adequate synchronization.

A data race occurs when multiple threads access the same memory location concurrently, at least one access writes, and no adequate synchronization protects the access. The result can include corrupted data, instability, and subtle bugs. section 2.4

A data race is a specific memory error; a race condition is broader: the outcome depends on uncontrolled timing. Eliminating data races does not guarantee deterministic results, because thread order or database-call duration can still change the outcome. section 2.0 section 2.4

// Thread 1 code
coffee.price = 10
// Thread 2 code
print(coffee.price)

An unsynchronized write and read of the same price can produce an unpredictable result. section 2.4

// Thread 1 code
mutex.Lock()
coffee.price = 10
mutex.Unlock()
// Thread 2 code
mutex.Lock()
print(coffee.price)
mutex.Unlock()

A mutex serializes access to the price and removes the data race, although execution order can still leave the printed value nondeterministic. section 2.4

Safe access does not ensure a fixed result

If one thread writes coffee.price as 10 while another prints it, unsynchronized access creates a data race. Locking both operations with a mutex removes that memory error, but the printer may still run first and show the initial price. A brief incorrect like count may be harmless; an outdated bank balance is more serious. section 2.4

How The Coder Cafe covers it

How deep it goes
Defines the memory-access pattern, contrasts it with race conditions, and demonstrates mutex-based mitigation without descending into implementation internals.
Examples
Uses a shared coffee price, mutex-protected access, social-media likes, and an outdated bank balance.
What it assumes
Assumes basic awareness of threads and concurrent execution while distinguishing concurrency from parallelism and introducing mutexes and semaphores.
What you can do after
Diagnose unsynchronized shared-memory access and apply a mutex while recognizing that race conditions may remain.
How it connects
Connects data races with concurrency, parallelism, race conditions, mutexes, semaphores, and synchronization.

Also explained in

Compare how 5 books explain data race →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Coder Cafe. The section links point to the parts of the book it draws on.