summaryrefslogtreecommitdiff
path: root/tests/chan-test.hs
blob: 8d81e8ffcb21e54eb456c05a746d4c9f7c617db1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import Control.Concurrent (forkIO)
#if defined(STRICT)
import Control.Concurrent.Chan.Strict
#else
import Control.Concurrent.Chan
#endif
import System.Environment

-- Fork some computation processes, print their results
main = do
    n <- getArgs >>= readIO . head
    f1 <- run fibonacci
    f2 <- run fibonacci2
    mapM_ print . take n $ zip f1 f2

  -- fork a process, return any messages it produces as a list
  where
    run f = do
        c <- newChan
        l <- getChanContents c
        forkIO (writeList2Chan c f)
        return l

-- lazily returns values to the main thread to compute, should run twice
-- as fast if we compute in the worker thread
fibonacci = 0 : 1 : zipWith (+) fibonacci (tail fibonacci)

fibonacci2 = 1 : 1 : zipWith (+) fibonacci2 (tail fibonacci2)