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
