phasync
Channels and messaging / Queue

Class

Queue

A first-in first-out queue whose operations take a lock, so coroutines may share it.

Introduction#

Nothing here waits for a value: tryDequeue() returns false when the queue is empty. To wait for values, use a channel.

Class synopsis#

class Queue implements QueueInterface, LockInterface, \Countable {
    /* Methods */
    public Queue::__construct()
    public Queue::isEmpty(): bool
    public Queue::enqueue(mixed $value): void
    public Queue::tryDequeue(mixed &$value): bool
    public Queue::tryPeek(mixed &$value): bool
    public Queue::count(): int
    public Queue::lock(Closure $callable, ?float $timeout = null): mixed
}

Methods#

MethodDescription
__constructCreates an empty queue.
isEmptyReturns true if the queue holds no values.
enqueueAdds $value at the end of the queue.
tryDequeueTakes the first value off the queue into $value, and returns whether there was one.
tryPeekCopies the first value of the queue into $value, without removing it, and returns whether there was one.
countReturns the number of values in the queue.
lockRuns $callable while holding the lock of this object, and returns what it returns.

Examples#

Example 1
$queue = new phasync\Util\Queue();
$queue->enqueue('a');
$queue->enqueue('b');

$queue->tryDequeue($value);   // $value is 'a'
echo count($queue), "\n";     // 1

See also#