[Haskell-cafe] Concurrency strategy for 2 threads and rare events

Erik Hesselink hesselink at gmail.com
Wed Feb 8 17:11:43 CET 2012


You could use throwTo to raise an exception in the thread you want to
stop. Otherwise, having some variable (IORef, TVar, MVar) that the
long running thread occasionally checks seems like a good solution.

Erik

On Wed, Feb 8, 2012 at 17:04, JP Moresmau <jpmoresmau at gmail.com> wrote:
> Hello, I'm wondering what's the best strategy to use in the following scenario:
> - 2 threads
>  - One perform some work that will take time, possibly go on forever
>  - Another waits for user input (like commands from the keyboard)
> that affects thread 1 (causing it to stop, in the simplest case)
>
> I've read a bit on MVar and channels, but they seem to be a lot for
> cases where the reading thread block for input. In my case, I expect
> to have "something" that thread 2 updates when an event occur, and
> thread 1 checks it regularly. So thread 1 should not block, but should
> check "is there something" and there is, act on it, otherwise continue
> doing what it was currently doing. I suppose I could just tryTakeMVar
> on a MVar, but is there something more adapted to my needs?
>
> Thanks!
>
> --
> JP Moresmau
> http://jpmoresmau.blogspot.com/
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list