https://github.com/simre1/multitasking
Easy concurrency in Haskell
https://github.com/simre1/multitasking
Last synced: 10 months ago
JSON representation
Easy concurrency in Haskell
- Host: GitHub
- URL: https://github.com/simre1/multitasking
- Owner: Simre1
- License: bsd-3-clause
- Created: 2025-06-05T21:35:35.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-07-03T15:58:10.000Z (about 1 year ago)
- Last Synced: 2025-07-22T00:03:07.302Z (12 months ago)
- Language: Haskell
- Size: 42 KB
- Stars: 11
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Multitasking
Haskell has great tools for dealing with concurrency. However, in praxis they are difficult to use.
This library aims to make concurrency easy by providing many built-in solutions for common concurrency patterns.
It is based on [structured concurrency](https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful), not letting threads outlive their parent scope. Additionally, exceptions are propagated automatically. This means that you do not have to worry about:
- Ghost processes, since a thread can never outlive its parent scope.
- Dead processes, since exceptions will propagate to the parent thread.
## Examples
```haskell
awaitAllExample :: IO ()
awaitAllExample =
-- open up a concurrency scope
multitask $ \coordinator -> do
-- launch tasks
task1 <- start coordinator action1
task2 <- start coordinator action2
-- Wait for all actions to complete
await coordinator
```
```haskell
awaitTaskExample :: IO ()
awaitTaskExample =
-- open up a concurrency scope
multitask $ \coordinator -> do
-- start task
task <- start coordinator $ pure 10
-- do some work
let x = 10
-- wait for task to complete and get the result
result <- await task
-- prints 20
print $ x + result
```
```haskell
raceTasksExample :: IO ()
raceTasksExample = multitask $ \coordinator ->
slot <- newSlot
_ <- start coordinator $ action1 >>= putSlot slot
_ <- start coordinator $ action2 >>= putSlot slot
result <- awaitSlot slot
print result
```
```haskell
builtinRaceExample :: IO ()
builtinRaceExample = do
result <- raceTwo (threadDelay 1000000 >> pure 10) (pure 20)
print result
```
## Comparison with other libraries
- `ki`: Implements structured concurrency, but provides no high-level functionality. `multitasking` uses `ki` internally and additionally implements commonly used patterns.
- `async`: Does not implement structured concurrency
## Acknowledgements
- Inspired by [Notes on structured concurrency](https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful), which is implemented in the `trio` Python library.