synchronizing MVar ( was compiling concurrent haskell with ghc )
Thu, 31 Jul 2003 01:40:18 -0400 (EDT)
On Wed, 30 Jul 2003, Dennis Sidharta wrote:
> Hi again,
> I have tried to play around with MVar today, but
> still... I did not see how to synchronize MVar without
> the help of forkIO, and then explicitly call yield ::
> IO (). But then, if I use forkIO I will create "daemon
> threads" that will cause my program to terminates
> immediately (back to the original problem).
The Haskell program terminates when the main thread is done. Your main
thread is done as soon as it forks the two other threads. If you want to
be sure your additional threads get to run, you must have the main thread
wait for them to finish (using one or more MVars, of course). See the
section "Terminating the program" in the documentation for the
> I attached the source code to this email.
> ps: If you can tell the depth of my understanding of
> concurrent Haskell from my code, please do not
> hesitate to evaluate it (Eg. am I using MVar
> correctly? Any better method? etc.), since I am really
> new to concurrent Haskell. Thanks.
If you want your two additional threads to have a proper dialogue, you
need *two* MVars. (This is not a Haskell issue but a concurrency issue.)
Having fixed that, you'll find that you don't need (or want) the `yield`s.
Your code has lots of repeated patterns. Time to abstract a bit more!
> Again, thank you for the help.
> Dennis Sidharta
> --- Sven Panne <Sven.Panne@informatik.uni-muenchen.de>
> > Dennis Sidharta wrote:
> > > [ problems with concurrent Haskell ]
> > I can see two problems in your code:
> > * forkIO creates "daemon threads", so the program
> > terminates immediately.
> > * Chan is an unbounded channel, so you won't get a
> > "ping pong", which
> > is probably what you expected. MVar is your
> > friend here.
> > See
> > Cheers,
> > S.