phasync
Channels and messaging / WriteChannelInterface

Interface

WriteChannelInterface

The writing end of a channel: write() sends a value to the readers of the channel.

Introduction#

On an unbuffered channel write() returns when a reader has taken the value; on a buffered one it returns at once while the buffer has room, and waits when it is full. Dropping the last reference to an end closes the channel, so a reader sees the end when the writer is gone.

Class synopsis#

interface WriteChannelInterface extends SelectableInterface {
    /* Methods */
    public WriteChannelInterface::activate(): void
    public WriteChannelInterface::close(): void
    public WriteChannelInterface::isClosed(): bool
    public WriteChannelInterface::write(mixed $value, float $timeout = PHP_FLOAT_MAX): void
    public WriteChannelInterface::isWritable(): bool
}

Methods#

MethodDescription
activateSwitches off the deadlock check of the channel.
closeCloses the channel, for both ends.
isClosedReturns true if the channel has been closed.
writeSends a value, and waits while the channel cannot take it: for a reader on an unbuffered channel, for room on a full buffer.
isWritableReturns true if the channel is open, so a write is accepted.

Examples#

Example 1
phasync::run(function () {
    phasync::channel($read, $write, 3);

    phasync::go(function () use ($write) {
        foreach ([1, 2, 3] as $number) {
            $write->write($number);
        }
        $write->close();
    });

    foreach ($read as $number) {
        echo $number, "\n";
    }
});

See also#

  • phasync::channelCreates a channel and returns its two ends in $read and $write.
  • ReadChannelInterfaceThe reading end of a channel: values written to the channel arrive here in the order they were written.