unsafeInterleaveIO

Simon Marlow simonmar@microsoft.com
Mon, 11 Aug 2003 13:28:49 +0100


=20
> On Mon, Aug 11, 2003 at 12:15:57PM +0100, Simon Marlow wrote:
> > I'm sure you're right, but I don't understand why an unsafePerformIO
> > has to have its own thread at all.
>=20
> In Hugs, threads exist only in the IO monad.
>=20
> > It's fine for an unsafePerformIO to run to completion before
> > anything else happens - in fact that's exactly what I'd expect in a
> > single-threaded system.
>=20
> Sure, but is that what you'd expect of unsafeInterleaveIO?

Certainly!

Consider the common use of unsafeInterleaveIO to return the contents of
a file lazilly.  You would write something like this:

   getContents =3D unsafeInterleaveIO f=20
     where f =3D do c <- getChar=20
                  xs <- unsafeInterleaveIO f
                  return (c:xs)

as you can see, it's fine for each invocation of f to run to completion,
because it uses nested unsafeInterleaveIO to get the interleaving.

Cheers,
	Simon