[Haskell-cafe] STM, orElse and timed read from a channel

Tomasz Zielonka tomasz.zielonka at gmail.com
Tue Nov 29 01:28:52 EST 2005

On Mon, Nov 28, 2005 at 11:27:46PM +0000, Joel Reymont wrote:
> Folks,
> How would you implement a timed read from a channel with STM? I would  
> like to return Timeout if nothing was read from a TChan in X ms.
> Is this a basic two-thread timeout implementation or is there a more  
> elegant way of implementing this using `orElse`?

Here is a basic two-thread implementation:

The other approach I see is to create a TVar that would be updated with
current time every 0.1 s (or something like that), but that would
be rather inefficient.

You could also create a time-out manager thread (with a priority queue,
etc), so you don't have to spawn a thread for every timeout.

Best regards

I am searching for a programmer who is good at least in some of
[Haskell, ML, C++, Linux, FreeBSD, math] for work in Warsaw, Poland

More information about the Haskell-Cafe mailing list