4.25. Thread::Queue Class

Note: This class is not available with the PO_NO_THREAD_CLASSES parse option.

Queue objects provide a blocking, thread-safe message-passing object to Qore programs.

Table 4.749. Queue Method Overview

Method

Except?

Description

Queue::constructor()

N

Creates the Queue object.

Queue::destructor()

Y

Destroys the Queue object.

Queue::copy()

N

Creates a new Queue object with the same elements as the original.

any Queue::get(timeout $timeout_ms = 0)

Y

Blocks until at least one entry is available on the queue, then returns the first entry in the queue. If a timeout occurs, an exception is thrown. If the timeout is less than or equal to zero, then the call does not timeout until data is available.

any Queue::pop(timeout $timeout_ms = 0)

Y

Blocks until at least one entry is available on the queue, then returns the last entry in the queue. If a timeout occurs, an exception is thrown. If the timeout is less than or equal to zero, then the call does not timeout until data is available.

nothing Queue::push(any $val)

N

Puts a value on the end of the queue.

int Queue::size()

N

Returns the number of elements in the queue.

int Queue::getWaiting()

N

Returns the number of threads currently blocked on this queue.


4.25.1. Queue::constructor()

Synopsis

Creates the Queue object.

Prototype

Queue::constructor()

Example
my Queue $queue();

4.25.2. Queue::destructor()

Synopsis

Destroys the object. Note that it is a programming error to delete this object while other threads are blocked on it; in this case an exception is thrown in the deleting thread, and in each thread blocked on this object when it is deleted.

Example
delete $queue;

Table 4.750. Exceptions Thrown by Queue::destructor()

err

desc

QUEUE-ERROR

The queue was deleted while other threads were blocked on it.


4.25.3. Queue::copy()

Synopsis

Creates a new Queue object with the same elements as the original.

Example
my Queue $new_queue = $queue.copy();

4.25.4. Queue::get()

Synopsis

Blocks until at least one entry is available on the queue, then returns the first entry in the queue. Accepts an optional timeout value in milliseconds (1/1000 second). Like all Qore functions and methods taking timeout values, a relative date/time value may be passed instead of an integer to make the timeout units clear (ex: 2500ms for 2.5 seconds). If no value or a value that converts to integer 0 is passed as the argument, then the call does not timeout until data is available on the queue.

Note that this function will throw an exception on timeout, in order to enable the case where NOTHING was pushed on the queue to be differentiated from a timeout.

Prototype

any Queue::get(timeout $timeout_ms = 0)

Example
$data = $queue.get();

Table 4.751. Arguments for Queue::get()

Argument

Description

timeout $timeout_ms = 0

An optional timeout value to wait for data to become available on the queue; integers are interpreted as milliseconds; relative date/time values are interpreted literally. If no value or a value that converts to integer 0 is passed as the argument, then the call does not timeout until data is available on the queue. If a non-zero timeout argument is passed, and no data is available in the timeout period, a QUEUE-TIMEOUT exception is thrown.


Table 4.752. Return Values for Queue::get()

Return Type

Description

any

Depends on the value put on the queue; could also be NOTHING, because NOTHING can be pushed as a value on the queue.


Table 4.753. Exceptions Thrown by Queue::get()

err

desc

QUEUE-TIMEOUT

The timeout value was exceeded.

QUEUE-ERROR

The queue was deleted in another thread while this thread was blocked on it.


4.25.5. Queue::pop()

Synopsis

Blocks until at least one entry is available on the queue, then returns the last entry in the queue. Accepts an optional timeout value in ms (1/1000 second). Like all Qore functions and methods taking timeout values, a relative date/time value may be passed instead of an integer to make the timeout units clear (ex: 2500ms for 2.5 seconds). If no value or a value that converts to integer 0 is passed as the argument, then the call does not timeout until data is available on the queue.

Note that this function will throw an exception on timeout, in order to enable the case where NOTHING was pushed on the queue to be differentiated from a timeout.

Prototype

any Queue::pop(timeout $timeout_ms = 0)

Example
$data = $queue.pop();

Table 4.754. Arguments for Queue::pop()

Argument

Description

timeout $timeout_ms = 0

An optional timeout value to wait for data to become available on the queue; integers are interpreted as milliseconds; relative date/time values are interpreted literally. If no value or a value that converts to integer 0 is passed as the argument, then the call does not timeout until data is available on the queue. If a non-zero timeout argument is passed, and no data is available in the timeout period, a QUEUE-TIMEOUT exception is thrown.


Table 4.755. Return Values for Queue::pop()

Return Type

Description

any

Depends on the value put on the queue.


Table 4.756. Exceptions Thrown by Queue::pop()

err

desc

QUEUE-TIMEOUT

The timeout value was exceeded.

QUEUE-ERROR

The queue was deleted in another thread while this thread was blocked on it.


4.25.6. Queue::push()

Synopsis

Adds a value to the end of the queue.

Prototype

nothing Queue::push(any $val)

Example
$queue.push($value);

Table 4.757. Arguments for Queue::push()

Argument

Description

any $val

Value to be put on the queue.


4.25.7. Queue::size()

Synopsis

Returns the number of elements in the queue.

Prototype

int Queue::size()

Example
my int $size = $queue.size();

Table 4.758. Return Values for Queue::size()

Return Type

Description

int

The number of elements in the queue.


4.25.8. Queue::getWaiting()

Synopsis

Returns the number of threads currently blocked on this queue.

Prototype

int Queue::getWaiting()

Example
my int $num = $queue.getWaiting();

Table 4.759. Return Values for Queue::getWaiting()

Return Type

Description

int

The number of threads currently blocked on this queue.