FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
pool::ThreadPool Class Reference

A simple thread pool implementation. More...

#include "thread_pool.h"

Public Member Functions

 ThreadPool (unsigned numThreads)
 Constructs a ThreadPool with a specified number of threads.
 
 ~ThreadPool ()
 Destroys the ThreadPool, joining all threads.
 
 ThreadPool (const ThreadPool &)=delete
 
ThreadPooloperator= (const ThreadPool &)=delete
 
 ThreadPool (ThreadPool &&)=delete
 
ThreadPooloperator= (ThreadPool &&)=delete
 
template<class F , class... Args>
std::future< std::invoke_result_t< F, Args... > > enqueue (F &&f, Args &&... args)
 Enqueues a task to be executed by the thread pool.
 
void wait ()
 Waits for all tasks in the thread pool to finish.
 
unsigned getAvailableThreads ()
 Returns the number of threads available for executing tasks.
 

Detailed Description

A simple thread pool implementation.

Definition at line 28 of file thread_pool.h.

Constructor & Destructor Documentation

◆ ThreadPool() [1/3]

pool::ThreadPool::ThreadPool ( unsigned  numThreads)
explicit

Constructs a ThreadPool with a specified number of threads.

Parameters
numThreadsThe number of threads in the pool.

Definition at line 18 of file thread_pool.cpp.

19 {
20 _workers.reserve(numThreads);
21 for (unsigned i = 0; i < numThreads; ++i)
22 {
23 _workers.emplace_back(
24 [this]
25 {
26 while (true)
27 {
28 Task task;
29 {
30 std::unique_lock lock(_queue_mutex);
31 _condition.wait(lock, [this] { return _stop || !_tasks.empty(); });
32 if (_stop && _tasks.empty())
33 {
34 return;
35 }
36 task = std::move(_tasks.front());
37 _tasks.pop();
38 }
39 try
40 {
41 task();
42 }
43 catch (const std::exception& e)
44 {
45 LOG(logging::Level::ERROR, "Exception in thread pool: {}", e.what());
46 }
47
48 {
49 std::unique_lock lock(_queue_mutex);
50 --_pending_tasks;
51 if (_pending_tasks == 0)
52 {
53 _done_condition.notify_all();
54 }
55 }
56 }
57 });
58 }
59 }
#define LOG(level,...)
Definition logging.h:19
@ ERROR
Error level for error events.

References logging::ERROR, and LOG.

◆ ~ThreadPool()

pool::ThreadPool::~ThreadPool ( )

Destroys the ThreadPool, joining all threads.

Definition at line 61 of file thread_pool.cpp.

62 {
63 {
64 std::unique_lock lock(_queue_mutex);
65 _stop = true;
66 _condition.notify_all();
67 }
68 for (std::thread& worker : _workers)
69 {
70 worker.join();
71 }
72 }

◆ ThreadPool() [2/3]

pool::ThreadPool::ThreadPool ( const ThreadPool )
delete

◆ ThreadPool() [3/3]

pool::ThreadPool::ThreadPool ( ThreadPool &&  )
delete

Member Function Documentation

◆ enqueue()

template<class F , class... Args>
std::future< std::invoke_result_t< F, Args... > > pool::ThreadPool::enqueue ( F &&  f,
Args &&...  args 
)

Enqueues a task to be executed by the thread pool.

Template Parameters
FThe type of the function to be executed.
ArgsThe types of the arguments to the function.
Parameters
fThe function to be executed.
argsThe arguments to the function.
Returns
A future that will hold the result of the function.
Exceptions
std::runtime_errorif the thread pool is stopped.

Definition at line 57 of file thread_pool.h.

58 {
59 using ReturnType = std::invoke_result_t<F, Args...>;
60
61 auto task = std::make_shared<std::packaged_task<ReturnType()>>(
62 std::bind(std::forward<F>(f), std::forward<Args>(args)...));
63
64 std::future<ReturnType> res = task->get_future();
65 {
66 std::unique_lock lock(_queue_mutex);
67 if (_stop)
68 {
69 throw std::runtime_error("enqueue on stopped ThreadPool");
70 }
71
72 ++_pending_tasks;
73 _tasks.emplace([task] { (*task)(); });
74 _condition.notify_one();
75 }
76 return res;
77 }

◆ getAvailableThreads()

unsigned pool::ThreadPool::getAvailableThreads ( )

Returns the number of threads available for executing tasks.

Returns
The number of available threads.

Definition at line 74 of file thread_pool.cpp.

75 {
76 std::unique_lock lock(_queue_mutex);
77 const unsigned active_threads = _pending_tasks;
78 const unsigned total_threads = _workers.size();
79 return total_threads > active_threads ? total_threads - active_threads : 0;
80 }

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ wait()

void pool::ThreadPool::wait ( )

Waits for all tasks in the thread pool to finish.

Definition at line 82 of file thread_pool.h.

83 {
84 std::unique_lock lock(_queue_mutex);
85 _done_condition.wait(lock, [this] { return _pending_tasks == 0; });
86 }

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