unfork: Make any action thread safe

[ apache, concurrency, library ] [ Propose Tags ]

“Unfork” is the opposite of “fork”; whereas forking allows things to run concurrently, unforking prevents things from running concurrently. Use one of the functions in the Unfork module when you have an action that will be used by concurrent threads but needs to run serially.

A typical use case is a multi-threaded program that writes log messages. If threads use putStrLn directly, the strings may be interleaved in the combined output. Instead, create an unforked version of putStrLn:

import Unfork

main :: IO ()
main =
    unforkAsyncIO_ putStrLn \putStrLn' ->
        _ -- Within this continuation, use
          -- putStrLn' instead of putStrLn

The new putStrLn' function writes to a queue. A separate thread reads from the queue and performs the actions, thus ensuring that the actions are all performed in one linear sequence. The main thread returns after the continuation has returned and the queue is empty. If an exception is raised in either thread, both threads halt and the exception is re-raised in the main thread.


[Index] [Quick Jump]


Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

For package maintainers and hackage trustees


  • No Candidates
Versions [RSS]
Dependencies async (>=2.2.2 && <2.3), base (>=4.14 && <4.17), safe-exceptions (>=0.1.7 && <0.2), stm (>=2.5 && <2.6) [details]
License Apache-2.0
Copyright 2022 Mission Valley Software LLC
Author Chris Martin
Maintainer Chris Martin, Julie Moronuki
Revised Revision 1 made by chris_martin at 2022-05-31T18:19:54Z
Category Concurrency
Home page https://github.com/typeclasses/unfork
Bug tracker https://github.com/typeclasses/unfork/issues
Source repo head: git clone git://github.com/typeclasses/unfork.git
Uploaded by chris_martin at 2022-05-31T18:17:57Z
Distributions NixOS:, Stackage:
Downloads 28 total (15 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2022-05-31 [all 1 reports]