Skip to content

Commit 4a97f17

Browse files
committed
fix, much cleanup and doc
1 parent 7576633 commit 4a97f17

File tree

7 files changed

+268
-76
lines changed

7 files changed

+268
-76
lines changed

cabal.project

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
packages: unfork
1+
packages: unfork, unfork-demo

unfork-demo/Demo.hs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Demo where
2+
3+
import Prelude (IO, String, putStr, show, (*>), (<>))
4+
import Control.Concurrent (threadDelay)
5+
import Control.Concurrent.Async (concurrently_)
6+
import Data.Foldable (for_)
7+
import Numeric.Natural (Natural)
8+
9+
putStrLnSlow :: String -> IO ()
10+
putStrLnSlow = r
11+
where
12+
r [] = putStr "\n"
13+
r (x : xs) = putStr [x] *> threadDelay 10 *> r xs
14+
15+
logFromTwoThreads :: (String -> IO ()) -> IO ()
16+
logFromTwoThreads log =
17+
concurrently_
18+
(for_ [1 .. 9 :: Natural] (\i -> log ("A " <> show i <> "\tone two three four") *> threadDelay 50))
19+
(for_ [1 .. 9 :: Natural] (\i -> log ("B " <> show i <> "\tfive six seven eight") *> threadDelay 200))
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Main (main) where
2+
3+
import Demo
4+
import System.IO
5+
6+
main :: IO ()
7+
main = do
8+
hSetBuffering stdout NoBuffering
9+
logFromTwoThreads putStrLnSlow

unfork-demo/logging/unforked/Main.hs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Main (main) where
2+
3+
import Demo
4+
import System.IO
5+
import Unfork
6+
7+
main :: IO ()
8+
main = do
9+
hSetBuffering stdout NoBuffering
10+
unforkAsyncIO_ putStrLnSlow logFromTwoThreads

unfork-demo/unfork-demo.cabal

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
cabal-version: 3.0
2+
3+
name: unfork-demo
4+
version: 0
5+
6+
common base
7+
default-language: Haskell2010
8+
ghc-options: -Wall
9+
build-depends: async, base, unfork
10+
11+
library
12+
import: base
13+
exposed-modules: Demo
14+
15+
executable demo-logging-interleaved
16+
import: base
17+
build-depends: unfork-demo
18+
hs-source-dirs: logging/interleaved
19+
main-is: Main.hs
20+
21+
executable demo-logging-unforked
22+
import: base
23+
build-depends: unfork-demo
24+
hs-source-dirs: logging/unforked
25+
main-is: Main.hs

0 commit comments

Comments
 (0)