58void demo_basic_usage()
60 std::cout <<
"[1] Single-threaded push/try_pop walkthrough\n";
70 std::cout <<
"popped " <<
out <<
"\n";
71 std::cout <<
"queue empty: " << std::boolalpha << q.
is_empty() <<
"\n\n";
74void demo_multiple_producers()
76 std::cout <<
"[2] Four producers feeding one consumer\n";
79 constexpr int producer_count = 4;
80 constexpr int items_per_producer = 5000;
83 std::atomic<bool> start{
false};
84 std::vector<std::thread> producers;
86 for (
int p = 0; p < producer_count; ++p)
87 producers.emplace_back([&, p]
89 while (not start.load(std::memory_order_acquire))
91 for (int i = 0; i < items_per_producer; ++i)
95 std::vector<int> per_producer_count(producer_count, 0);
96 const int total = producer_count * items_per_producer;
99 start.store(
true, std::memory_order_release);
103 while (consumed < total)
108 ++per_producer_count[
static_cast<size_t>(job.producer)];
112 for (
auto & t : producers)
115 std::cout <<
"Consumed " << consumed <<
" jobs from " << producer_count
117 for (
int p = 0; p < producer_count; ++p)
118 std::cout <<
" producer " << p <<
": " << per_producer_count[
static_cast<size_t>(p)]
119 <<
" jobs (expected " << items_per_producer <<
")\n";
120 std::cout <<
"queue empty after drain: " << std::boolalpha << q.
is_empty()
127 std::cout <<
"\n=== Aleph::MpscQueue: multi-producer/single-consumer queue ===\n\n";
130 demo_multiple_producers();
132 std::cout <<
"Done.\n";
size_t size_t int32_t * out
Unbounded lock-free multi-producer/single-consumer queue.
void emplace(Args &&... args)
Construct a new element in place at the back of the queue.
void push(const T &value)
Push a copy of value onto the queue.
bool is_empty() const noexcept
Advisory check for whether the queue currently has no elements.
bool try_pop(T &out)
Attempt to pop the front element into out.
Main namespace for Aleph-w library functions.
void print_rule()
Prints a horizontal rule for example output separation.
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
Unbounded lock-free multi-producer/single-consumer queue (Aleph::MpscQueue).