thread-hierarchy: Simple Haskel thread management in hierarchical manner

[ concurrency, library, mit ] [ Propose Tags ]

Threads created by newChild guarantee automatic cleanup on its exit regardless normal exit or cancellation by asynchronous exception.

[Skip to Readme]


Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


  • No Candidates
Versions [RSS],,,,,,,
Change log
Dependencies base (>=4.7 && <5), containers, lifted-base, monad-control, transformers-base [details]
License MIT
Copyright Copyright: (c) 2017 Naoto Shimazaki
Author Naoto Shimazaki
Category Concurrency
Home page
Source repo head: git clone
Uploaded by nshimaza at 2017-10-02T06:52:20Z
Distributions LTSHaskell:, NixOS:, Stackage:
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 4994 total (31 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2017-10-02 [all 1 reports]

Readme for thread-hierarchy-

[back to package description]


License: MIT Build Status

Managing Haskell threads in hierarchical manner.

Threads created by newChild guarantee automatic cleanup on its exit regardless normal exit or cancellation by asynchronous exception.

In order to this module works properly, user must ensure following rules.

  • User provided thread handler must accept ThreadMap as its first argument.
  • When new thread created by newChild, handler receives newly created empty ThreadMap.
  • When the user provided handler creates its child thread, it must use newChild.
  • At the same time, the handler must pass the ThreadMap received via its argument to newChild.

ThreadMap is mutable map holding live threads. Each threads managed by this module has its own ThreadMap instance. Each ThreadMap keeps live "child" threads for future cleanup on exit. Populating ThreadMap is done by newChild automatically. Actually the function newChild accept a ThreadMap then mutate it.

At the same time newChild create a new empty ThreadMap for newly created thread and pass it to user provided handler of the new thread. Also newChild automatically install cleanup routine which kill all threads living in the new ThreadMap created for the thread. The cleanup routine also unregister itself from ThreadMap of parent.

In order to work this properly, user provided thread handler must use newChild with ThreadMap given via its argument when it creates its child so that cleanup routine can terminate its children properly.


When you create the first thread managed by this module, create a new empty ThreadMap then call newChild with it. The newCall automatically install cleanup routine to the handler you provided.

createRootThread :: IO ThreadId
createRootThread = do
    rootThreadMap <- newThreadMap
    threadID <- newChild rootThreadMap rootThreadHandler
    return threadID

When a thread managed by this module creates its child thread, call newChild with TreadMap received via handlers argument.

rootThreadHandler :: ThreadMap -> IO ()
rootThreadHandler myChildrenThreadMap = do
    void $ newChild myChildrenThreadMap $ \grandChildrenThreadMap -> do
        return ()

You can install your own cleanup routine using finally or both resource acquisition and cleanup routine using bracket.

    -- Forking a new child with your own cleanup
    void $ newChild childrenOfCurrentThread $ \childrenOfHandler ->
        yourThreadHandler `finally` yourCleanupRoutine

    -- Forking a new child with resource acquisition and cleanup
    void $ newChild childrenOfCurrentThread $ \childrenOfHandler ->
        bracket yourResourceAcquiringRoutine yourCleanupRoutine yourThreadHandler


Currently, unlike async function, this module is not designed to back any return value from child thread to parent thread. This module focuses on guaranteed cleanup on thread termination.