Skip to content

Instantly share code, notes, and snippets.

@boris-stepanov
Created September 29, 2021 00:12
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 boris-stepanov/481821f9fdf93cc3076157a1e8b912a9 to your computer and use it in GitHub Desktop.
Save boris-stepanov/481821f9fdf93cc3076157a1e8b912a9 to your computer and use it in GitHub Desktop.
-- stack ghc logger.hs --resolver lts-18.5 -- -O2
-- htop shows 600M leak even when tchans are empty
import Prelude hiding (read)
import Control.Monad
import Control.Concurrent
import Control.Concurrent.STM.TChan
import Control.Concurrent.STM
c :: Int
c = 10000000
write :: TChan String -> IO ()
write ch = do
replicateM_ c (atomically $ writeTChan ch "string")
read :: TChan String -> IO ()
read ch = replicateM_ c (atomically (readTChan ch))
main :: IO ()
main = do
(w, r) <- atomically $ do
w <- newBroadcastTChan
r <- dupTChan w
pure (w, r)
forkIO (threadDelay (10^6) >> read r >> print "Reading done") -- Wait till the queue is full
write w >> print "Writing done"
threadDelay (2 * 10^6)
atomically (isEmptyTChan r) >>= print
threadDelay (100 * 10^6) -- Prevent exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment