liveBook · Manning

Concurrency primitive

As explained in Grokking Simplicity

A synchronization construct that provides a defined operation for coordinating concurrent execution paths or managing shared resources.

A concurrency primitive is reusable functionality for sharing resources or coordinating concurrent execution paths. It limits undesirable orderings so concurrent code reaches a correct result. section 16.0 section 16.6

Common concurrency primitives make different timing guarantees:

  • Queue or linearize calls so one asynchronous action runs at a time and tasks complete in order. section 16.6
  • DroppingQueue keeps only a bounded number of pending tasks, skipping stale work when newer work arrives. section 16.9
  • Cut waits for a specified number of timelines to finish before continuing. section 17.10
  • JustOnce wraps an action so repeated calls produce an effect only on the first call. section 17.15

Serializing cart-total updates

A queue accepts cart states, removes the oldest pending item, and runs its worker only when no earlier item is active. After the worker finishes, it starts the next item. Wrapping this logic in Queue(worker) separates reusable coordination from application-specific work. section 16.4 section 16.6

function Cut(num, callback) { #1 
    var num_finished = 0; #2
    return function() {
        num_finished += 1; #3
        if(num_finished === num)
            callback(); #4
    };
}

Inspect the compact Cut implementation: it closes over a completion counter and invokes the callback when the final timeline reports completion. section 17.10

Timeline diagrams help diagnose timing bugs before choosing a primitive. The design process is incremental: isolate shared coordination, encode the desired ordering or repetition, then generalize the reusable portion. section 16.11 section 17.17

How Grokking Simplicity covers it

How deep it goes
Builds queues, dropping queues, Cut, and JustOnce incrementally, including ordering analysis and JavaScript implementation details.
Examples
Demonstrates cart-total queues, dropping document saves, parallel web requests, completion barriers, and one-time text messages.
What it assumes
Assumes JavaScript actions, callbacks, asynchronous events, and timelines while teaching resource sharing, ordering, and repetition explicitly.
What you can do after
Build it from scratch, diagnose timing bugs with timeline diagrams, and refactor application-specific coordination into reusable primitives.
How it connects
Connects concurrency primitives with timelines, shared resources, race conditions, higher-order functions, first-class values, queues, locks, and promises.

Also explained in

Compare how 2 books explain concurrency primitive →

Related concepts

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