Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
concurrency_utils_example.cc
Go to the documentation of this file.
1
9#include <concurrency_utils.H>
10#include <thread_pool.H>
11
12#include <algorithm>
13#include <iostream>
14#include <vector>
15
16using namespace Aleph;
17
18int main()
19{
20 std::cout << "Example 1: bounded_channel with TaskGroup\n";
24 ThreadPool pool(4);
25 TaskGroup group(pool);
26
27 group.launch([&] {
28 for (int i = 1; i <= 8; ++i)
29 jobs.send(i);
30 jobs.close();
31 });
32
33 for (int worker = 0; worker < 2; ++worker)
34 group.launch([&] {
35 while (auto item = jobs.recv())
36 {
37 squares.with_lock([&](auto & out) {
38 out.push_back(*item * *item);
39 });
40 processed.with_write_lock([](int & value) { ++value; });
41 }
42 });
43
44 group.wait();
45
46 auto sorted = squares.with_lock([](const auto & out) {
47 auto copy = out;
48 std::sort(copy.begin(), copy.end());
49 return copy;
50 });
51
52 std::cout << "Squares:";
53 for (const int value : sorted)
54 std::cout << " " << value;
55 std::cout << "\nProcessed count: "
56 << processed.with_read_lock([](const int & value) { return value; })
57 << "\n\n";
58
59 std::cout << "Example 2: cancellation-aware recv\n";
62 stop.request_cancel();
63 try
64 {
65 (void) canceled.recv(stop.token());
66 std::cout << "unexpected success\n";
67 }
68 catch (const operation_canceled &)
69 {
70 std::cout << "recv interrupted by cancellation\n";
71 }
72
73 std::cout << "\nExample 3: spsc_queue handoff\n";
75 handoff.try_push(10);
76 handoff.try_push(20);
77 auto first = handoff.try_pop();
78 auto second = handoff.try_pop();
79 std::cout << "Popped:";
80 if (first.has_value())
81 std::cout << " " << *first;
82 if (second.has_value())
83 std::cout << " " << *second;
84 std::cout << "\n";
85
86 return 0;
87}
Bounded blocking channel for producer-consumer workflows.
Cooperative cancellation source paired with CancellationToken.
CancellationToken token() const noexcept
Return a token observing this source.
void request_cancel() noexcept
Request cancellation for all derived tokens.
Read/write-lock protected shared object wrapper.
Bounded single-producer/single-consumer queue.
Mutex-protected shared object wrapper.
Minimal structured-concurrency helper over ThreadPool futures.
A reusable thread pool for efficient parallel task execution.
Exception thrown when cooperative cancellation is observed.
Modern synchronization helpers for channels, shared state, and small producer-consumer queues.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
STL namespace.
A modern, efficient thread pool for parallel task execution.