Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dmjio/tstack
A concurrent, thread-safe, transactional stack
https://github.com/dmjio/tstack
haskell stack stm
Last synced: 2 months ago
JSON representation
A concurrent, thread-safe, transactional stack
- Host: GitHub
- URL: https://github.com/dmjio/tstack
- Owner: dmjio
- License: bsd-3-clause
- Created: 2018-11-15T22:45:14.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2018-11-21T13:45:34.000Z (about 6 years ago)
- Last Synced: 2024-04-15T14:22:51.095Z (9 months ago)
- Topics: haskell, stack, stm
- Language: Haskell
- Homepage:
- Size: 384 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
- License: LICENSE
Awesome Lists containing this project
README
# tstack
A concurrent, thread-safe, transactional Stack### Build
```
nix-build
```### Develop
```
$ nix-shell --command 'runghc Setup.hs build'
```### Example
```haskell
module Main whereimport Control.Monad
import Control.Concurrent
import Control.Concurrent.STM
import Control.Concurrent.STM.TStackimport GHC.Conc
-- | This example illustrates GHC's
-- fairness policy, and models the
-- "hungry-hungry hippos" model of
-- concurrent consumption
main :: IO ()
main = do
tstack <- newTStack
forkIO . forever . void $ do
-- Will block until we get an item
item <- atomically (pop tstack)
showThread
putStrLn $ "You entered: " <> item
forkIO . forever . void $ do
-- Will block until we get an item
item <- atomically (pop tstack)
showThread
putStrLn $ "You entered: " <> item
forever $ do
line <- getLine
-- Will push an item when we get it
atomically (push line tstack)
where
showThread = do
tid <- myThreadId
putStrLn $ "Printed by Thread: " <> show tid
```### Result
```bash
$ nix-build && ./result/bin/example +RTS -N
a
Printed by Thread: ThreadId 2
You entered: a
b
Printed by Thread: ThreadId 3
You entered: b
c
Printed by Thread: ThreadId 2
You entered: c
d
Printed by Thread: ThreadId 3
You entered: d
e
Printed by Thread: ThreadId 2
You entered: e
```