xtd 1.0.0
Loading...
Searching...
No Matches
concurrent_queue.cpp

Shows how to use xtd::collections::concurrent::concurrent_queue class.

#include <xtd/xtd>
class example {
public:
// Demonstrates:
// concurrent_queue<type_t>::enqueue()
// concurrent_queue<type_t>::try_peek()
// concurrent_queue<type_t>::try_dequeue()
static auto main() {
// Construct a concurrent_queue.
auto cq = concurrent_queue<int> {};
// Populate the queue.
for (auto i = 0; i < 10000; ++i)
cq.enqueue(i);
// Peek at the first element.
auto result = 0;
if (!cq.try_peek(result))
console::write_line("CQ: try_peek failed when it should have succeeded");
else if (result != 0)
console::write_line("CQ: Expected try_peek result of 0, got {0}", result);
auto outer_sum = 0;
// An action to consume the concurrent_queue.
auto action = [&cq, &outer_sum] {
auto local_sum = 0;
auto local_value = 0;
while (cq.try_dequeue(local_value)) local_sum += local_value;
interlocked::add(outer_sum, local_sum);
};
// Start 4 concurrent consuming actions.
parallel::invoke(action, action, action, action);
console::write_line("outer_sum = {0}, should be 49995000", outer_sum);
}
};
startup_(example::main);
// This code produces the following output :
//
// outer_sum = 49995000, should be 49995000
Represents a thread-safe first in-first out (FIFO) collection.
Definition concurrent_queue.hpp:34
virtual auto enqueue(const_reference item) -> void
Adds an item to the end of the xtd::collections::concurrent::concurrent_queue <type_t>.
Definition concurrent_queue.hpp:136
static auto write_line() -> void
Writes the current line terminator to the standard output stream using the specified format informati...
#define startup_(...)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:152
Provides several thread-safe collection classes that should be used in place of the corresponding typ...
Definition concurrent_bag.hpp:20