[Haskell-cafe] Replacing stdin from within Haskell
Richard O'Keefe
ok at cs.otago.ac.nz
Fri Jun 10 06:43:40 CEST 2011
On 10/06/2011, at 1:11 AM, Erik Hesselink wrote:
> On Thu, Jun 9, 2011 at 13:40, Neil Davies <semanticphilosopher at gmail.com> wrote:
>> Anyone out there got an elegant solution to being able to fork a haskell thread and replace its 'stdin' ?
>
> If you don't mind being tied to GHC you can use hDuplicateTo from
> GHC.IO.Handle [1]. You can also use dupTo from the unix package [2],
> but that ties you to unix-like platforms instead.
>
> Erik
>
> [1] http://hackage.haskell.org/packages/archive/base/latest/doc/html/GHC-IO-Handle.html#v:hDuplicateTo
That replaces stdin for *every* Haskell thread.
> [2] http://hackage.haskell.org/packages/archive/unix/latest/doc/html/System-Posix-IO.html#v:dupTo
That alters file descriptor 0 for *every* Haskell thread.
As I understand the original poster's question, it would be
paraphrased as "can the association between the default input stream
used by getChar and the rest be changed for ONE thread in a Haskell
program by some method that does NOT have any effect on any other
thread in the same OS process". Or to put it another way, "do I
really have to pass a handle around to all these functions in my
thread?"
And the answer would seem to be "there is no reason in principle
why this *couldn't* be supported, but it *isn't*; there are no
C-style thread-local global variables. Yes, you do have to pass
that handle around more than you would like to."
More information about the Haskell-Cafe
mailing list