packages feed

debuggable-0.1.0: demo/Demo/NIIO.hs

module Demo.NIIO (
    withoutDebuggable
  , useDebuggable
  ) where

import Control.Concurrent
import Control.Concurrent.Async
import Control.Monad
import System.IO

import Debug.NonInterleavedIO qualified as NIIO

{-------------------------------------------------------------------------------
  Without the library
-------------------------------------------------------------------------------}

withoutDebuggable :: IO ()
withoutDebuggable = do
    hSetBuffering stdout NoBuffering

    concurrently_
      ( replicateM_ 10 $ do
          putStrLn "This is a message from the first thread"
          threadDelay 100_000
      )
      ( replicateM_ 10 $ do
          putStrLn "And this is a message from the second thread"
          threadDelay 100_000
      )

{-------------------------------------------------------------------------------
  Using the library
-------------------------------------------------------------------------------}

useDebuggable :: IO ()
useDebuggable = do
    concurrently_
      ( replicateM_ 10 $ do
          NIIO.putStrLn "This is a message from the first thread"
          threadDelay 100_000
      )
      ( replicateM_ 10 $ do
          NIIO.putStrLn "And this is a message from the second thread"
          threadDelay 100_000
      )