Skip to content

Instantly share code, notes, and snippets.

@rrnewton
Created April 2, 2013 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rrnewton/5294361 to your computer and use it in GitHub Desktop.
Save rrnewton/5294361 to your computer and use it in GitHub Desktop.
An example of a concurrency bug that is upstream from the point of the concurrentMerge.
import qualified System.IO.Streams as S
import System.IO.Streams.Concurrent
import Control.Concurrent
import Data.IORef
import Data.ByteString.Char8 as B
main = do
cnt <- newIORef 0
s1 <- S.makeInputStream $ do
c <- readIORef (cnt :: IORef Int)
threadDelay$ 100 * 1000
writeIORef cnt (c+1)
if c >= 100
then return Nothing
else return$ Just c
s2 <- S.map (Left) s1
s3 <- S.map (Right) s1
m <- concurrentMerge [s2,s3]
hd <- S.take 10 m
ls <- S.toList hd
print ls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment