[Haskell-cafe] Re: I/O interface

Keean Schupke k.schupke at imperial.ac.uk
Thu Jan 13 04:44:10 EST 2005


No I meant Channels (from Data.Concurrent)... you can use a structure like:

    data Command = Read FileAddr (MVar MyData) | Write FileAddr MyData

So to write you just do:

    writeChan iochan (Write address data) -- returns immediately
    -- write happens asynchronously later

and to read:

    data <- newEmptyMVar
    writeChan iochan (Read address data) -- read not happend yet.
    myData <- readMVar data -- blocks until read completes.

The forked thread (with forkIO) just reads the commands form the "iochan"
and processes them one at a time.

    Keean

Ketil Malde wrote:

>Keean Schupke <k.schupke at imperial.ac.uk> writes:
>
>  
>
>>At the end of the day IO is serial by nature (to one device anyway),
>>so the way to do this into one file is to have one thread that reads
>>and writes, and to 'send' read and write requests over channels from
>>the threads that need the work done
>>    
>>
>
>Would the stream proposal make this possible and easy?  I.e. could the
>IO thread provide (say) output streams to the other threads, and pass
>writes on to its own output stream?
>
>-kzm
>  
>



More information about the Haskell-Cafe mailing list