Skip to content
API Documentation
BastionGuard™
ThreadPool Class Reference

Public Member Functions

 ThreadPool (size_t n, size_t max_queue=4096)
 ~ThreadPool ()
void enqueue (std::function< void()> fn)

Private Member Functions

void worker_thread ()

Private Attributes

std::vector< std::thread > workers
std::queue< std::function< void()> > tasks
std::mutex mtx
std::condition_variable cv
std::condition_variable cv_space
bool stop
size_t max_queue

Detailed Description

Definition at line 65 of file BastionGuard-ransomware-scanner.cpp.

Constructor & Destructor Documentation

◆ ThreadPool()

ThreadPool::ThreadPool ( size_t n,
size_t max_queue = 4096 )
inlineexplicit

Definition at line 67 of file BastionGuard-ransomware-scanner.cpp.

68 : stop(false), max_queue(max_queue)
69 {
70 for (size_t i = 0; i < n; ++i)
71 workers.emplace_back([this]{ worker_thread(); });
72 }
std::vector< std::thread > workers

References max_queue, stop, worker_thread(), and workers.

◆ ~ThreadPool()

ThreadPool::~ThreadPool ( )
inline

Definition at line 74 of file BastionGuard-ransomware-scanner.cpp.

74 {
75 {
76 std::unique_lock<std::mutex> lk(mtx);
77 stop = true;
78 }
79 cv.notify_all();
80 cv_space.notify_all();
81 for (auto &t : workers)
82 if (t.joinable()) t.join();
83 }
std::condition_variable cv_space
std::condition_variable cv

References cv, cv_space, mtx, stop, and workers.

Member Function Documentation

◆ enqueue()

void ThreadPool::enqueue ( std::function< void()> fn)
inline

Definition at line 85 of file BastionGuard-ransomware-scanner.cpp.

85 {
86 std::unique_lock<std::mutex> lk(mtx);
87 cv_space.wait(lk, [this]{ return stop || tasks.size() < max_queue; });
88 if (stop) return;
89 tasks.push(std::move(fn));
90 cv.notify_one();
91 }
std::queue< std::function< void()> > tasks

References cv, cv_space, max_queue, mtx, stop, and tasks.

Referenced by main().

◆ worker_thread()

void ThreadPool::worker_thread ( )
inlineprivate

Definition at line 94 of file BastionGuard-ransomware-scanner.cpp.

94 {
95 for (;;) {
96 std::function<void()> fn;
97 {
98 std::unique_lock<std::mutex> lk(mtx);
99 cv.wait(lk, [this]{ return stop || !tasks.empty(); });
100 if (stop && tasks.empty())
101 return;
102 fn = std::move(tasks.front());
103 tasks.pop();
104 cv_space.notify_one();
105 }
106 fn();
107 }
108 }

References cv, cv_space, mtx, stop, and tasks.

Referenced by ThreadPool().

Member Data Documentation

◆ cv

std::condition_variable ThreadPool::cv
private

Definition at line 113 of file BastionGuard-ransomware-scanner.cpp.

Referenced by enqueue(), worker_thread(), and ~ThreadPool().

◆ cv_space

std::condition_variable ThreadPool::cv_space
private

Definition at line 114 of file BastionGuard-ransomware-scanner.cpp.

Referenced by enqueue(), worker_thread(), and ~ThreadPool().

◆ max_queue

size_t ThreadPool::max_queue
private

Definition at line 116 of file BastionGuard-ransomware-scanner.cpp.

Referenced by enqueue(), and ThreadPool().

◆ mtx

std::mutex ThreadPool::mtx
private

Definition at line 112 of file BastionGuard-ransomware-scanner.cpp.

Referenced by enqueue(), worker_thread(), and ~ThreadPool().

◆ stop

bool ThreadPool::stop
private

◆ tasks

std::queue<std::function<void()> > ThreadPool::tasks
private

Definition at line 111 of file BastionGuard-ransomware-scanner.cpp.

Referenced by enqueue(), and worker_thread().

◆ workers

std::vector<std::thread> ThreadPool::workers
private

Definition at line 110 of file BastionGuard-ransomware-scanner.cpp.

Referenced by ThreadPool(), and ~ThreadPool().


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