[mercury-users] channel vs. stream

Peter Ross pro at missioncriticalit.com
Wed Dec 18 22:36:54 AEDT 2002


On Wed, Dec 18, 2002 at 06:02:58PM +1100, Michael Day wrote:
> 
> Channels defined in extras/concurrency/channel are very similar to
> streams, except that they are infinite and reading from one will always
> return a value, if it does not block indefinitely.
> 
> If channels could be closed, after which no further writes are possible
> and reads return yes(T) until all the buffered items have been read and no
> forever afterwards, would this unnecessarily complicate the interface? Is
> the infinite nature of channels an intentional design goal that matches
> the way they are used, or just an unintended side effect of implementation
> choices?
> 
It was a deliberate design decision.

However one could easily implement a closable channel on top of channel.

:- type closable_channel(T)
    --->    closable_channel(bool, mvar(int), channel(T)).

closable_channel__take(ClosableChannel, Result) -->
    { ClosableChannel = closable_channel(Open, NumItemsMvar, Channel) },
    ( { Open = yes },
        channel__take(Channel, T),
        mvar__take(NumItemsMvar, NumItems),
        mvar__put(NumItemsMvar, NumItems - 1)
        { Result = yes(T) }
    ; { Open = no },
        mvar__take(NumItemsMvar, NumItems),
        ( { NumItems = 0 } ->
            { Result = no }
            mvar__put(NumItemsMvar, NumItems)
        ;
            channel__take(Channel, T),
            { Result = yes(T) },
            mvar__put(NumItemsMvar, NumItems - 1)
        )
    ).

In fact now that I think about it, it is not so easy to write.  The
problem is the following.

Time    Thread 1                        Thread 2
1       closable_channel__take
2                                       closable_channel__close

Now no-one can put an item onto the channel for thread 1 to take because
the closable_channel is closed and thread 1 will block indefinitely.  So
what should happen for this situation?
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list