queue
#include <async/queue.hpp>
queue
is a type which provides a queue on which you can asynchronously wait
for items to appear.
Prototype
template <typename T, typename Allocator>
struct queue {
queue(Allocator allocator = {}); // (1)
void put(T item); // (2)
template <typename ...Ts>
void emplace(Ts &&...ts); // (3)
sender async_get(cancellation_token ct = {}); // (4)
frg::optional<T> maybe_get() // (5)
};
- Constructs a queue with the given allocator.
- Inserts an item into the queue.
- Emplaces an item into the queue.
- Returns a sender for the get operation. The operation waits for an item to be inserted and returns it.
- Pops and returns the top item if it exists, or
frg::null_opt
otherwise.
Requirements
T
is moveable. Allocator
is an allocator.
T
is constructible withTs
.
Arguments
allocator
- the allocator to use.item
- the item to insert into the queue.ts
- the arguments to pass to the constructor ofT
when inserting it into the queue.ct
- the cancellation token to use.
Return values
- N/A
- This method doesn't return any value.
- Same as (2).
- This method returns a sender of unspecified type. The sender returns a
frg::optional<T>
and completes with the value, orfrg::null_opt
if the operation was cancelled. - This method returns a value of type
frg::optional<T>
. It returns a value from the queue, orfrg::null_opt
if the queue is empty.
Examples
auto coro = [] (async::queue<int, frg::stl_allocator> &q) -> async::detached {
std::cout << "Got " << *(co_await q.async_get()) << std::endl;
std::cout << "Got " << *(co_await q.async_get()) << std::endl;
};
async::queue<int, frg::stl_allocator> q;
coro(q);
q.put(1);
q.put(2);
Output:
1
2