|
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 <iostream>#include <iomanip>#include <vector>#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_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 blocking✅ 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 368 of file thread_pool_example.cc.
References Aleph::ThreadPool::enqueue_bounded_detached(), Aleph::ThreadPool::get_queue_limits(), Aleph::maps(), 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 168 of file thread_pool_example.cc.
References Aleph::ThreadPool::enqueue(), is_prime(), Aleph::maps(), Aleph::ThreadPool::num_threads(), print_header(), and Aleph::HTList::size().
Referenced by main().
| void example_batch_processing | ( | ) |
Definition at line 258 of file thread_pool_example.cc.
References Aleph::ThreadPool::enqueue_bulk(), Aleph::DynList< T >::get(), Aleph::maps(), Aleph::ThreadPool::num_threads(), print_header(), process_file(), and Aleph::HTList::size().
Referenced by main().
| void example_fire_and_forget | ( | ) |
Definition at line 322 of file thread_pool_example.cc.
References Aleph::ThreadPool::enqueue_detached(), Aleph::maps(), print_header(), and Aleph::ThreadPool::wait_all().
Referenced by main().
| void example_load_shedding | ( | ) |
Definition at line 446 of file thread_pool_example.cc.
References Aleph::maps(), print_header(), Aleph::ThreadPool::set_queue_limits(), Aleph::ThreadPool::try_enqueue_detached(), and Aleph::ThreadPool::wait_all().
Referenced by main().
| void example_performance | ( | ) |
Definition at line 499 of file thread_pool_example.cc.
References Aleph::ThreadPool::enqueue(), Aleph::DynList< T >::get(), Aleph::maps(), Aleph::ThreadPool::num_threads(), and print_header().
Referenced by main().
| bool is_prime | ( | int | n | ) |
Definition at line 158 of file thread_pool_example.cc.
Referenced by example_basic_parallel(), and example_parallel_filter().
| int main | ( | ) |
Definition at line 566 of file thread_pool_example.cc.
References example_backpressure(), example_basic_parallel(), example_batch_processing(), example_fire_and_forget(), example_load_shedding(), and example_performance().
| void print_header | ( | const std::string & | title | ) |
Definition at line 133 of file thread_pool_example.cc.
References Aleph::maps().
| FileResult process_file | ( | const std::string & | filename | ) |
Definition at line 244 of file thread_pool_example.cc.
References Aleph::maps().
Referenced by example_batch_processing().