dev
dev

Reputation: 1179

Haskell ForkIO limit number of threads to certain value

Seems like ForkIO creates as many threads as there are cores in the Haskell program I work with

-- | Fork a thread in checker monad.
fork :: Checker a b () -> Checker a b ()
fork act = do
  s0 <- get
  void $ liftIO $ forkIO (curTGroup s0) $ evalChecker act s0

occurs :: Eq a => a -> [a] -> Int
occurs x = length . filter (x==)

https://github.com/PLSysSec/sys/blob/821c4d7cf924e68838c128cbe824be46c9955416/src/Static/Check.hs#L73

New to Haskel ForkIO, I wanted to set the thread amount using setNumCapabilities.

Tried adding

let setNumCapabilities = 1

Haskell made a warning about unused var and this didn't make any effect.

How to do it properly?

Upvotes: 1

Views: 246

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476574

setNumCapabilities :: Int -> IO () is a function. You thus use it in your code with:

import Control.Concurrent(setNumCapabilities)

fork :: Checker a b () -> Checker a b ()
fork act = do
  s0 <- get
  void $ liftIO $ do
    setNumCapabilities 1
    forkIO (curTGroup s0) $ evalChecker act s0

or somewhere else, for example in the main function.

Upvotes: 2

Related Questions