isEmptyChan blocks?

Simon Marlow simonmar@microsoft.com
Mon, 23 Jun 2003 11:05:44 +0100


=20
> In local.glasgow-haskell-users, you wrote:
> > That makes isEmptyChan essentially useless.  Either it should be
> > removed, or the implementation of Chan needs to be=20
> elaborated to support
> > isEmptyChan.
>=20
> I have a new implementation here which should track the=20
> number of elements
> in a channel, taking also into account unGetChan & dupChan.=20
> It seems to
> work, but lacks thorough testing. In any case, as it bloats=20
> the data structure
>=20
> data Chan a
>  =3D Chan (MVar ([MVar Integer],Stream a))
>         (MVar ([MVar Integer],Stream a))
> 	(MVar Integer)
>=20
> type Stream a =3D MVar ([MVar Integer],ChItem a)
>=20
> it should probably be something like Control.Concurrent.CountChan.
>=20
> http://www.foldr.org/~stolz/Chan2.hs

Could you explain how this works?  writeChan doesn't update the count -
it should, right?  But if it does, won't there be race conditions when
read & write happen at the same time?

> [although I consider
>    mapM_ (uncurry putMVar) (zip counts (map (+1) vs))
>  rather neat :)]

Not as neat as

  zipWithM_ putMVar counts (map (+1) vs)

:-p

Cheers,
	Simon