liveBook · Manning

Retry

As explained in Functional Design and Architecture: Examples in Haskell

Retry is an STM operation that rolls back a transaction and waits for relevant transactional variables to change before restarting it.

retry is an STM operation for expressing “wait until this transactional state becomes acceptable.” It rolls back the current transaction, restores transactional variables to their initial values, and restarts after a relevant variable changes. section 9.2.1

Waiting for a flag

A transaction can read a TVar Bool, return 100 when the flag is True, and call retry when it is False. STM then blocks the thread until the flag changes instead of repeatedly polling it. section 9.2.1

Use retry when a transaction cannot proceed safely:

  • Roll back every transactional update made during the failed attempt. section 9.2.1
  • Let STM track the variables read by the transaction and resume it after an observed change. section 9.2.1 section 9.2.2
  • Avoid indefinite waits by ensuring another thread can eventually change the required state. section 9.2.1

Capacity-limited process execution

A runtime counter can call retry when its current process count equals the maximum, then increment the counter once capacity becomes available. This replaces manual polling and delay logic with blocking STM code. section 9.2.2

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Explains rollback, blocking, restart conditions, and polling avoidance, but does not detail the STM runtime’s internal wait algorithm.
Examples
Uses a Boolean flag, a bounded process counter, and dining-philosopher fork acquisition to demonstrate conditional waiting.
What it assumes
Assumes familiarity with STM transactions and TVar reads, while establishing rollback and blocking behavior through concrete examples.
What you can do after
Apply it via Haskell STM to replace polling with rollback-safe blocking transactions.
How it connects
Connects STM with transactions, TVar, atomically, concurrency, and resource-capacity management.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.