[Haskell-cafe] ANN: threads-0.2

Bas van Dijk v.dijk.bas at gmail.com
Wed Jun 2 05:27:57 EDT 2010


I just released threads-0.2. The library which lets you fork threads
and wait for their result.

$ cabal update
$ cabal install threads


darcs get http://code.haskell.org/~basvandijk/code/threads

* I removed the regular 'Control.Concurrent.ThreadId' out of our own
'ThreadId α' type and renamed the type to 'Result α':
data ThreadId α = ThreadId
    { result   ∷ TMVar (Either SomeException α)
    , threadId ∷ Control.Concurrent.ThreadId
newtype Result α = Result
    { unResult ∷ TMVar (Either SomeException α) }

* The various fork functions now return the regular 'ThreadId' paired
with a 'Result α' value:
forkIO ∷ IO α → IO (ThreadId α)  to:
forkIO ∷ IO α → IO (ThreadId, Result α)

* The various wait functions now just receive a  'Result α' value:
wait ∷ ThreadId α → IO (Either SomeException α)  to:
wait ∷ Result α → IO (Either SomeException α)

* I removed:
throwTo ∷ Exception e ⇒ ThreadId α → e → IO ()  and:
killThread ∷ ThreadId α → IO ()
because they aren't needed anymore since we have direct access to the
regular 'ThreadId'.

* When you build the package with GHC the GHC specific 'forkOnIO'
function is additionally build in:
forkOnIO ∷ Int → IO α → IO (ThreadId, Result α)

* For the rest I added documentation and made some internal changes
like a simplified implementation of ThreadGroups and made small
stylistic changes all over the place.

Inspired by the 'parIO' combinator from the 'async' package, I'm
thinking about adding a module which offers similar functionality:

data ParGroup α
new ∷ IO (ParGroup α)
forkIO ∷ ParGroup α → IO α → IO ThreadId
wait ∷ ParGroup α → IO (Maybe α)

The idea is that when you have multiple IO computations, that each
compute the same answer but differ in how fast they compute that
answer, you can compute them in parallel using 'forkIO'. Finally
'wait' blocks until one of the threads has computed an answer and will
return Just that answer. When all of the threads fail to compute an
answer (throw an exception) wait returns Nothing.



More information about the Haskell-Cafe mailing list