unfork
Version 1.0.0.0 revision 0 uploaded by chris_martin.
Package meta
- Synopsis
- Make any action thread safe
- Description
“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.
- Author
- Chris Martin
- Bug reports
- https://github.com/typeclasses/unfork/issues
- Category
- Concurrency
- Copyright
- 2022 Mission Valley Software LLC
- Homepage
- https://github.com/typeclasses/unfork
- Maintainer
- Chris Martin, Julie Moronuki
- Package URL
- n/a
- Stability
- n/a