Re: [Haskell-cafe] Implementing tryReadMVar

tryReadMVar mv = do mc <- tryTakeMVar mv
The normal reason people want tryRead is to do something like unix's 'select' function, where you want to wait on one of several signals... In my opinion it is better to do this with a _single_ channel and have one thread taking from the channel, whilst all sources of the 'events' write to the same channel... so the refactoring would be like: data Event = Even1 | Event2 | Event3 ... c <- newChan forkIO (...) a <- readChan c case a of Event1 -> ... Event2 -> ... Keean.

MR K P SCHUPKE wrote:
tryReadMVar mv = do mc <- tryTakeMVar mv
The normal reason people want tryRead is to do something like unix's 'select' function, where you want to wait on one of several signals...
Combining the channels into one is certainly a bit nicer, but there might be a reason to want to prefer one event over another. But wouldn't it be better to write this using just tryTakeMVar, rather than tryRead followed by blocking take? This would guarantee that the events matched, and that the code would continue to work as expected in the multiple-reader case. -Jan-Willem Maessen
participants (2)
-
Jan-Willem Maessen - Sun Labs East
-
MR K P SCHUPKE