You may want to consult the tutorial.
template<typename Container>
struct basic_unbounded_queue;
template<typename T, typename Allocator = std::allocator<T>>
using unbounded_queue = basic_unbounded_queue<std::vector<T, Allocator>>;
basic_unbounded_queue
is the simplest queue: it does not have the concept of being full. It allows the user to continue to add elements until the underlying container is unable to allocate any more space.
template<typename Container>
struct basic_blocking_queue;
template<typename T, typename Allocator = std::allocator<T>>
using blocking_queue = basic_blocking_queue<std::vector<T, Allocator>>;
basic_blocking_queue
accepts a size parameter in its constructor. If the queue already has at least max_size
elements when the user attempts to add data (by calling append
, emplace
, or push
), that call will block until the queue is reduced in size to have fewer than max_size
elements.
Parameter | Definition |
---|---|
Container | The underlying container that the queue uses to store its data. The requirements and exception guarantees for any operation on the queue exactly match the documented call to the underlying container, except where noted otherwise. Container must be DefaultConstructible |
Type | Definition |
---|---|
container_type | Container template parameter |
value_type | container_type::value_type |
Except where noted otherwise, all operations are thread-safe.
Some definitions refer to "container
" as though it were a data member. This value is for exposition only, and can be assumed to be a value of type container_type
that contains all current values in the queue.
The behavior of any function which accepts a time_point
or duration
parameter is undefined if any operations on the time_point
or duration
calls any member functions of the queue object.
Function | Definition |
---|---|
constructor | basic_unbounded_queue is default constructible. This constructs an empty queue. basic_blocking_queue is constructible from container_type::size_type . This constructs an empty queue and sets the max_size to that value. |
move constructor | Moves the contents of the queue and the max_size (if present) from the old queue to the new queue. Not thread safe, as the assumption is that the source of the move is a temporary. Only defined if container_type is MoveConstructible |
copy constructor | Deleted |
move assignment | Deleted |
copy assignment | Deleted |
destructor | Destroys the underlying container. The behavior is undefined if any threads are accessing the queue when the destructor is called. |
Adds elements to the underlying container by calling container.insert(container.end(), first, last)
.
This should be preferred over emplace
or push
where possible, as it typically leads to better performance.
Returns void
.
Elements added to the queue by this operation are guaranteed to be added atomically. No other elements will be added in the middle, and if a consumer pops everything off the queue, they will either see all of the elements in the range or none of them.
container.insert(container.end(), first, last)
must be a valid expression that adds all elements in the range [first, last)
to the end of the container. The behavior is undefined if calling container.insert(container.end(), first, last)
would call any member functions on this queue object.
Adds an element to the underlying container by calling container.emplace_back(std::forward<Args>(args)...)
. Returns void
.
container.emplace_back(std::forward<Args>(args)...)
must be a valid expression that adds a single item into the container. For std::vector
as the container_type
, this requires that value_type
is MoveInsertable and EmplaceConstructible. The behavior is undefined if calling container.emplace_back(std::forward<Args>(args)...)
would call any member functions on this queue object.
Equivalent to emplace(std::move(value))
Equivalent to emplace(value)
Blocks until there is data in the queue. Returns all messages in the queue. After this call, the queue's internal container is equal to the original value of storage
. This overload will never return an empty container.
container_type
must model the Swappable and MoveConstructible concepts. The behavior is undefined if using std::swap; swap(container, storage);
calls any member functions on this queue object.
Blocks until there is data in the queue or a stop request is made. Returns all messages in the queue. After this call, the queue's internal container is equal to the original value of storage
.
container_type
must model the Swappable and MoveConstructible concepts. The behavior is undefined if using std::swap; swap(container, storage);
calls any member functions on this queue object.
container_type pop_all(std::chrono::time_point<Clock, Duration> timeout, container_type storage = container_type{})
Blocks until there is data in the queue or timeout
is reached. Returns all messages in the queue. After this call, the queue's internal container is equal to the original value of storage
.
This function may block beyond timeout
due to scheduling or resource contention delays.
container_type
must model the Swappable and MoveConstructible concepts. The behavior is undefined if using std::swap; swap(container, storage);
calls any member functions on this queue object.
container_type pop_all(std::stop_token token, std::chrono::time_point<Clock, Duration> timeout, container_type storage = container_type{})
Blocks until there is data in the queue, a stop request is made, or timeout
is reached. Returns all messages in the queue. After this call, the queue's internal container is equal to the original value of storage
.
This function may block beyond timeout
due to scheduling or resource contention delays.
container_type
must model the Swappable and MoveConstructible concepts. The behavior is undefined if using std::swap; swap(container, storage);
calls any member functions on this queue object.
container_type pop_all(std::chrono::duration<Rep, Period> timeout, container_type storage = container_type{})
Blocks until there is data in the queue or timeout
time has passed. Returns all messages in the queue. After this call, the queue's internal container is equal to the original value of storage
.
A steady clock is used to measure the duration. This function may block for longer than timeout
due to scheduling or resource contention delays.
container_type
must model the Swappable and MoveConstructible concepts. The behavior is undefined if using std::swap; swap(container, storage);
calls any member functions on this queue object.
container_type pop_all(std::stop_token token, std::chrono::duration<Rep, Period> timeout, container_type storage = container_type{})
Blocks until there is data in the queue, a stop request is made, or timeout
time has passed. Returns all messages in the queue. After this call, the queue's internal container is equal to the original value of storage
.
A steady clock is used to measure the duration. This function may block for longer than timeout
due to scheduling or resource contention delays.
container_type
must model the Swappable and MoveConstructible concepts. The behavior is undefined if using std::swap; swap(container, storage);
calls any member functions on this queue object.
Returns all messages in the queue. After this call, the queue's internal container is equal to the original value of storage
. This overload will return an empty container if the queue is empty when try_pop_all
is called.
container_type
must model the Swappable and MoveConstructible concepts. The behavior is undefined if using std::swap; swap(container, storage);
calls any member functions on this queue object.
Blocks until there is data in the queue. Returns the first element in the queue and removes it from the queue.
value_type
must model the MoveConstructible concept. The behavior is undefined if the move constructor calls any member functions on this queue object.
The expression container.front()
must produce a value of (possible reference-qualified) value_type
. container.pop_front()
must be valid and remove the first element from the container. The behavior is undefined if container.front()
or container.pop_front()
calls any member functions on this queue object.
Blocks until there is data in the queue or a stop request is made. Returns the first element in the queue and removes it from the queue. If there is no element in the queue because a stop request was made, returns std::nullopt
.
value_type
must model the MoveConstructible concept. The behavior is undefined if the move constructor calls any member functions on this queue object.
The expression container.front()
must produce a value of (possible reference-qualified) value_type
. container.pop_front()
must be valid and remove the first element from the container. The behavior is undefined if container.front()
or container.pop_front()
calls any member functions on this queue object.
Blocks until there is data in the queue or timeout
is reached. Returns the first element in the queue and removes it from the queue. If there is no element in the queue because the timeout was reached, returns std::nullopt
.
This function may block beyond timeout
due to scheduling or resource contention delays.
value_type
must model the MoveConstructible concept. The behavior is undefined if the move constructor calls any member functions on this queue object.
The expression container.front()
must produce a value of (possible reference-qualified) value_type
. container.pop_front()
must be valid and remove the first element from the container. The behavior is undefined if container.front()
or container.pop_front()
calls any member functions on this queue object.
std::optional<value_type> pop_one(std::stop_token token, std::chrono::time_point<Clock, Duration> timeout)
Blocks until there is data in the queue, a stop request is made, or timeout
is reached. Returns the first element in the queue and removes it from the queue. If there is no element in the queue because a stop request was made or the timeout was reached, returns std::nullopt
.
This function may block beyond timeout
due to scheduling or resource contention delays.
value_type
must model the MoveConstructible concept. The behavior is undefined if the move constructor calls any member functions on this queue object.
The expression container.front()
must produce a value of (possible reference-qualified) value_type
. container.pop_front()
must be valid and remove the first element from the container. The behavior is undefined if container.front()
or container.pop_front()
calls any member functions on this queue object.
Blocks until there is data in the queue or timeout
time has passed. Returns the first element in the queue and removes it from the queue. If there is no element in the queue because the timeout was reached, returns std::nullopt
.
A steady clock is used to measure the duration. This function may block for longer than timeout
due to scheduling or resource contention delays.
value_type
must model the MoveConstructible concept. The behavior is undefined if the move constructor calls any member functions on this queue object.
The expression container.front()
must produce a value of (possible reference-qualified) value_type
. container.pop_front()
must be valid and remove the first element from the container. The behavior is undefined if container.front()
or container.pop_front()
calls any member functions on this queue object.
std::optional<value_type> pop_one(std::stop_token token, std::chrono::duration<Rep, Period> timeout)
Blocks until there is data in the queue, a stop request is made, or timeout
time has passed. Returns the first element in the queue and removes it from the queue. If there is no element in the queue because a stop request was made or the timeout was reached, returns std::nullopt
.
A steady clock is used to measure the duration. This function may block for longer than timeout
due to scheduling or resource contention delays.
value_type
must model the MoveConstructible concept. The behavior is undefined if the move constructor calls any member functions on this queue object.
The expression container.front()
must produce a value of (possible reference-qualified) value_type
. container.pop_front()
must be valid and remove the first element from the container. The behavior is undefined if container.front()
or container.pop_front()
calls any member functions on this queue object.
Returns the first element in the queue and removes it from the queue if the queue is not empty, otherwise, returns std::nullopt
.
value_type
must model the MoveConstructible concept. The behavior is undefined if the move constructor calls any member functions on this queue object.
The expression container.front()
must produce a value of (possible reference-qualified) value_type
. container.pop_front()
must be valid and remove the first element from the container. The behavior is undefined if container.front()
or container.pop_front()
calls any member functions on this queue object.
Calls container.clear()
.
The behavior is undefined is container.clear()
calls any member functions on this queue.
Calls container.reserve(size)
The behavior is undefined is container.reserve(size)
calls any member functions on this queue.
Returns container.size()
The behavior is undefined is container.size()
calls any member functions on this queue.
Returns the maximum size of the queue.
max_size
is defined only for basic_blocking_queue
, not for basic_unbounded_queue
. container_type::size_type
must model the CopyConstructible concept. The behavior is undefined if the copy constructor of container_type::size_type
calls any member functions on this queue object.
This library requires C++20 support. The specific features used require one of the following (or newer)
- clang 13.0.1
- gcc 12.1.0