Copyright | (c) Daniel Taskoff 2018 Georgi Lyubenov 2018 |
---|---|
License | MIT |
Maintainer | daniel.taskoff@gmail.com, godzbanebane@gmail.com |
Stability | experimental |
Safe Haskell | Safe |
Language | Haskell2010 |
Implementation of skip variables - a special case of skip channels, the difference being that a value stored in a skip variable can be read at most once, while a skip channel can have multiple readers (i.e. multiple readers can read the same value).
Writing into a skip variable doesn't block - if there's a value stored, it's overwritten. Reading, on the other hand, blocks, if there isn't a new value since the last read, or if the skip variable is empty.
Some examples:
s <- newEmptySVar :: IO (SVar Int) print =<< readSVar s -- blocks
s <- newSVar 42 :: IO (SVar Int) print =<< readSVar s -- prints 42 print =<< readSVar s -- blocks
The next few lines will print some of the numbers between 0 and 10000. Note that at least one number will be
printed, but many will be skipped, because print
ing is slow.
import Control.Concurrent (forkIO, killThread) import Control.Monad (forever) s <- newEmptySVar :: IO (SVar Int) tid <- forkIO $ forever $ print =<< readSVar s mapM_ (putSVar s) [0..10000] killThread tid
SVar
An SVar
(skip variable) is a variable which allows for non-blocking updates,
and blocking reads if the stored data has been read already, or if there is no data.