threads
threads copied to clipboard
Feature Request: Bounded Thread Group
ThreadGroup assumes that the group could be unbounded, but many uses require threads groups to have a bound to control concurrency and resource exhaustion.
I wrote a simple wrapper to achieve this.
module ThreadLimit where
import Control.Concurrent.Thread.Group (ThreadGroup)
import qualified Control.Concurrent.Thread as Thread
import qualified Control.Concurrent.Thread.Group as ThreadGroup
import Control.Concurrent
data ThreadLimit = ThreadLimit
{ maxSize :: Int
, threadGroup :: ThreadGroup
}
new :: Int -> IO ThreadLimit
new maxSize = do
threadGroup <- ThreadGroup.new
return ThreadLimit {..}
wait :: ThreadLimit -> IO ()
wait ThreadLimit {..} = ThreadGroup.wait threadGroup
forkIO :: ThreadLimit -> IO a -> IO (ThreadId, IO (Thread.Result a))
forkIO ThreadLimit {..} action = do
ThreadGroup.waitN maxSize threadGroup
ThreadGroup.forkIO threadGroup action
I want to propose the feature of changing the ThreadGroup API to be bounded. I think if a Integer was used instead of Int or perhaps Int64 if one wanted a seemingly unbounded ThreadGroup they could use an unrealistically high maxSize.
Alternatively I could release a small package or threads could include a BoundedThreadGroup module.
Thoughts?