File Scanner 1.0.0
A high-performance C++ malicious file scanner.
|
Manages a pool of worker threads to execute tasks concurrently. More...
#include <thread_pool.h>
Public Member Functions | |
ThreadPool (std::size_t num_threads=0) | |
Constructs a thread pool with a specified number of threads. | |
~ThreadPool () | |
Destructor. Initiates a graceful shutdown and joins all threads. | |
ThreadPool (const ThreadPool &)=delete | |
ThreadPool & | operator= (const ThreadPool &)=delete |
ThreadPool (ThreadPool &&)=delete | |
ThreadPool & | operator= (ThreadPool &&)=delete |
void | Stop () |
Initiates the shutdown of the thread pool. | |
template<class F , class... Args> | |
auto | Enqueue (F &&f, Args &&... args) -> std::future< std::invoke_result_t< F, Args... > > |
Enqueues a task for execution by a worker thread. | |
Private Member Functions | |
void | Worker () |
Private Attributes | |
std::vector< std::thread > | workers_ |
std::queue< std::function< void()> > | tasks_ |
std::mutex | queue_mutex_ |
std::condition_variable | condition_ |
std::atomic< bool > | stop_ {false} |
Manages a pool of worker threads to execute tasks concurrently.
This class creates a fixed number of threads upon construction and allows tasks to be enqueued for execution. It provides a graceful shutdown mechanism that can be initiated manually via Stop() or automatically in the destructor. Once stopped, no new tasks can be enqueued.
|
explicit |
Constructs a thread pool with a specified number of threads.
num_threads | The number of worker threads to create. If 0, it defaults to the number of hardware concurrency units, with a minimum of 1. |
scanner::ThreadPool::~ThreadPool | ( | ) |
Destructor. Initiates a graceful shutdown and joins all threads.
The destructor calls Stop() to signal workers to finish and then waits for all currently executing and queued tasks to complete.
|
delete |
|
delete |
auto scanner::ThreadPool::Enqueue | ( | F && | f, |
Args &&... | args | ||
) | -> std::future<std::invoke_result_t<F, Args...>> |
Enqueues a task for execution by a worker thread.
This method is thread-safe.
F | The type of the callable object. |
Args | The types of the arguments to the callable. |
f | The callable object (e.g., function, lambda). |
args | The arguments to be passed to the callable. |
std::runtime_error | if the pool has been stopped. |
|
delete |
|
delete |
void scanner::ThreadPool::Stop | ( | ) |
Initiates the shutdown of the thread pool.
Sets a flag that prevents new tasks from being enqueued and wakes up all worker threads. The workers will complete any remaining tasks in the queue and then exit. This method is idempotent and thread-safe.
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |