liveBook · Manning

Race condition

As explained in Functional Programming in Scala, Second Edition

A race condition is a concurrency defect in which a result depends on the timing or interleaving of operations from multiple threads.

A race condition is a concurrency defect in which a result depends on the timing or interleaving of operations from multiple threads. Non-blocking parallel combinators such as map2 are especially vulnerable when results arrive in different orders. section 7.3.4

Combining parallel results safely

A non-blocking map2 must wait for both Par results before applying f. An actor receives either result, processes one message at a time, stores the first arrival, and combines the values when the second arrives. This avoids unsynchronized access to shared intermediate state. section 7.3.4

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Explains the coordination problem and demonstrates an actor-based solution, while deferring detailed actor implementation internals.
Examples
Uses Par.map2, two asynchronously arriving results, and an actor that combines stored Option values.
What it assumes
Assumes familiarity with multiple threads, parallel computations, callbacks, and the Par.map2 combinator.
What you can do after
Diagnose/debug with actors by identifying arrival-order hazards and serializing coordination through messages.
How it connects
Connects race conditions with concurrency, actors, deadlocks, threads, and non-blocking parallelism.

Also explained in

Compare how 20 books explain race condition →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in Scala, Second Edition. The section links point to the parts of the book it draws on.