File Scanner 1.0.0
A high-performance C++ malicious file scanner.
Loading...
Searching...
No Matches
scanner::ThreadPool Class Reference

Manages a pool of worker threads to execute tasks concurrently. More...

#include <thread_pool.h>

Collaboration diagram for scanner::ThreadPool:
Collaboration graph

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
 
ThreadPooloperator= (const ThreadPool &)=delete
 
 ThreadPool (ThreadPool &&)=delete
 
ThreadPooloperator= (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}
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ ThreadPool() [1/3]

scanner::ThreadPool::ThreadPool ( std::size_t  num_threads = 0)
explicit

Constructs a thread pool with a specified number of threads.

Parameters
num_threadsThe number of worker threads to create. If 0, it defaults to the number of hardware concurrency units, with a minimum of 1.
Here is the call graph for this function:

◆ ~ThreadPool()

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.

Here is the call graph for this function:

◆ ThreadPool() [2/3]

scanner::ThreadPool::ThreadPool ( const ThreadPool )
delete

◆ ThreadPool() [3/3]

scanner::ThreadPool::ThreadPool ( ThreadPool &&  )
delete

Member Function Documentation

◆ Enqueue()

template<class F , class... Args>
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.

Template Parameters
FThe type of the callable object.
ArgsThe types of the arguments to the callable.
Parameters
fThe callable object (e.g., function, lambda).
argsThe arguments to be passed to the callable.
Returns
A std::future that will hold the result of the task's execution.
Exceptions
std::runtime_errorif the pool has been stopped.
Here is the caller graph for this function:

◆ operator=() [1/2]

ThreadPool & scanner::ThreadPool::operator= ( const ThreadPool )
delete

◆ operator=() [2/2]

ThreadPool & scanner::ThreadPool::operator= ( ThreadPool &&  )
delete

◆ Stop()

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.

Here is the caller graph for this function:

◆ Worker()

void scanner::ThreadPool::Worker ( )
private
Here is the caller graph for this function:

Member Data Documentation

◆ condition_

std::condition_variable scanner::ThreadPool::condition_
private

◆ queue_mutex_

std::mutex scanner::ThreadPool::queue_mutex_
private

◆ stop_

std::atomic<bool> scanner::ThreadPool::stop_ {false}
private

◆ tasks_

std::queue<std::function<void()> > scanner::ThreadPool::tasks_
private

◆ workers_

std::vector<std::thread> scanner::ThreadPool::workers_
private

The documentation for this class was generated from the following files: