Created
September 29, 2021 00:12
-
-
Save boris-stepanov/481821f9fdf93cc3076157a1e8b912a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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