|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Comprehensive ThreadPool Usage Examples: Parallel Task Execution. More...
#include <thread_pool.H>#include <concurrency_utils.H>#include <ah-errors.H>#include <iostream>#include <iomanip>#include <vector>#include <algorithm>#include <functional>#include <numeric>#include <stdexcept>#include <cmath>#include <chrono>#include <fstream>#include <sstream>Go to the source code of this file.
Classes | |
| struct | FileResult |
Functions | |
| void | print_header (const std::string &title) |
| bool | is_prime (int n) |
| void | example_basic_parallel () |
| FileResult | process_file (const std::string &filename) |
| void | example_batch_processing () |
| void | example_fire_and_forget () |
| void | example_backpressure () |
| void | example_load_shedding () |
| void | example_structured_concurrency () |
| void | example_parallel_building_blocks () |
| void | example_channels_and_shared_state () |
| void | example_performance () |
| int | main () |
Comprehensive ThreadPool Usage Examples: Parallel Task Execution.
This file demonstrates how to use the ThreadPool class effectively for parallel task execution. ThreadPool provides a high-level interface for managing worker threads and executing tasks concurrently, making it easier to parallelize computations.
A thread pool is a collection of worker threads that execute tasks from a queue. Instead of creating threads for each task, threads are reused, reducing overhead and improving performance.
Benefits:
enqueue()**: Submit task and get futureenqueue_bulk()**: Process collections in parallelenqueue_detached()**: Submit without waitingenqueue_bounded()**: Limit queue sizetry_enqueue()**: Submit without blockingTaskGroup**: launch related tasks and wait as a unitCancellationSource / CancellationToken**: cooperative stop requestswait()parallel_invoke()**: launch a small related set of taskspscan() / pexclusive_scan()**: prefix sums in parallelpmerge()**: merge sorted ranges with the same pool/options machinerybounded_channel<T>**: bounded producer-consumer handoff with closesynchronized<T>**: mutex-protected shared objectsrw_synchronized<T>**: read/write-lock protected shared objectsspsc_queue<T>**: bounded single-producer/single-consumer handoff✅ Good for:
❌ Not good for:
| Operation | Complexity | Notes |
|---|---|---|
| enqueue() | O(1) amortized | Queue insertion |
| Future.get() | O(1) | Wait for completion |
| Thread creation | O(1) | Done at pool creation |
This example has no command-line options; it runs all examples.
Or using CMake:
Definition in file thread_pool_example.cc.
| void example_backpressure | ( | ) |
Definition at line 390 of file thread_pool_example.cc.
References Aleph::divide_and_conquer_partition_dp(), Aleph::ThreadPool::enqueue_bounded_detached(), Aleph::ThreadPool::get_queue_limits(), Aleph::ThreadPool::pending_tasks(), print_header(), Aleph::ThreadPool::set_queue_limits(), and Aleph::ThreadPool::wait_all().
Referenced by main().
| void example_basic_parallel | ( | ) |
Definition at line 190 of file thread_pool_example.cc.
References Aleph::divide_and_conquer_partition_dp(), Aleph::ThreadPool::enqueue(), is_prime(), Aleph::ThreadPool::num_threads(), and print_header().
Referenced by main().
| void example_batch_processing | ( | ) |
Definition at line 280 of file thread_pool_example.cc.
References Aleph::divide_and_conquer_partition_dp(), Aleph::ThreadPool::enqueue_bulk(), Aleph::ThreadPool::num_threads(), print_header(), and process_file().
Referenced by main().
| void example_channels_and_shared_state | ( | ) |
Definition at line 623 of file thread_pool_example.cc.
References Aleph::count(), Aleph::divide_and_conquer_partition_dp(), and print_header().
Referenced by main().
| void example_fire_and_forget | ( | ) |
Definition at line 344 of file thread_pool_example.cc.
References Aleph::divide_and_conquer_partition_dp(), Aleph::ThreadPool::enqueue_detached(), print_header(), and Aleph::ThreadPool::wait_all().
Referenced by main().
| void example_load_shedding | ( | ) |
Definition at line 468 of file thread_pool_example.cc.
References Aleph::divide_and_conquer_partition_dp(), print_header(), Aleph::ThreadPool::set_queue_limits(), Aleph::ThreadPool::try_enqueue_detached(), and Aleph::ThreadPool::wait_all().
Referenced by main().
| void example_parallel_building_blocks | ( | ) |
Definition at line 568 of file thread_pool_example.cc.
References Aleph::divide_and_conquer_partition_dp(), options, Aleph::parallel_invoke(), Aleph::pexclusive_scan(), Aleph::pmerge(), Aleph::ParallelOptions::pool, print_header(), Aleph::product(), Aleph::pscan(), seed, and Aleph::sum().
Referenced by main().
| void example_performance | ( | ) |
Definition at line 698 of file thread_pool_example.cc.
References Aleph::divide_and_conquer_partition_dp(), Aleph::ThreadPool::enqueue(), Aleph::ThreadPool::num_threads(), and print_header().
Referenced by main().
| void example_structured_concurrency | ( | ) |
Definition at line 518 of file thread_pool_example.cc.
References ah_runtime_error, Aleph::completed(), Aleph::divide_and_conquer_partition_dp(), print_header(), and Aleph::CancellationSource::token().
Referenced by main().
| bool is_prime | ( | int | n | ) |
Definition at line 180 of file thread_pool_example.cc.
Referenced by example_basic_parallel(), and example_parallel_filter().
| int main | ( | ) |
Definition at line 765 of file thread_pool_example.cc.
References example_backpressure(), example_basic_parallel(), example_batch_processing(), example_channels_and_shared_state(), example_fire_and_forget(), example_load_shedding(), example_parallel_building_blocks(), example_performance(), and example_structured_concurrency().
| void print_header | ( | const std::string & | title | ) |
Definition at line 155 of file thread_pool_example.cc.
| FileResult process_file | ( | const std::string & | filename | ) |
Definition at line 266 of file thread_pool_example.cc.
References Aleph::divide_and_conquer_partition_dp().
Referenced by example_batch_processing().