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#
| Method | Description |
|---|---|
__construct | Creates an empty queue. |
isEmpty | Returns true if the queue holds no values. |
enqueue | Adds $value at the end of the queue. |
tryDequeue | Takes the first value off the queue into $value, and returns whether there was one. |
tryPeek | Copies the first value of the queue into $value, without removing it, and returns whether there was one. |
count | Returns the number of values in the queue. |
lock | Runs $callable while holding the lock of this object, and returns what it returns. |
Examples#
$queue = new phasync\Util\Queue();
$queue->enqueue('a');
$queue->enqueue('b');
$queue->tryDequeue($value); // $value is 'a'
echo count($queue), "\n"; // 1See also#
phasync::channelTo wait for valuesLockInterfaceAn object that runs a closure while holding a lock that other coroutines wait for.