How long does it take to create 1 million threads in Haskell?

后端 未结 4 1054
栀梦
栀梦 2021-01-31 21:55

What I understand, Haskell have green threads. But how light weight are they. Is it possible to create 1 million threads?

Or How long would it take for 100 000 threads?

4条回答
  •  清酒与你
    2021-01-31 21:59

    from here.

    import Control.Concurrent
    import Control.Monad
    
    n = 100000
    
    main = do
        left  <- newEmptyMVar
        right <- foldM make left [0..n-1]
        putMVar right 0    -- bang!
        x <- takeMVar left -- wait for completion
        print x
     where
        make l n = do
           r <- newEmptyMVar
           forkIO (thread n l r)
           return r
    
    thread :: Int -> MVar Int -> MVar Int -> IO ()
    thread _ l r = do
       v <- takeMVar r
       putMVar l $! v+1
    

    on my not quite 2.5gh laptop this takes less than a second.

    set n to 1000000 and it becomes hard to write the rest of this post because the OS is paging like crazy. definitely using more than a gig of ram (didn't let it finish). If you have enough RAM it would definitely work in the appropriate 10x the time of the 100000 version.

提交回复
热议问题