Bash++
Bash++ compiler internal documentation
ThreadPool.h
Go to the documentation of this file.
1
7#pragma once
8#include <condition_variable>
9#include <functional>
10#include <mutex>
11#include <queue>
12#include <vector>
13#include <thread>
14
25 private:
26 std::vector<std::thread> workers;
27 std::queue<std::function<void()>> tasks;
28 std::mutex queue_mutex;
29 std::condition_variable condition;
30 bool stop = false;
31 public:
32 explicit ThreadPool(size_t threads = std::thread::hardware_concurrency());
34
35 ThreadPool(const ThreadPool& other) = delete; // Non-copyable
36 ThreadPool& operator=(const ThreadPool& other) = delete;
37 ThreadPool(ThreadPool&& other) noexcept = delete; // Non-movable
38 ThreadPool& operator=(ThreadPool&& other) noexcept = delete;
39
45 void enqueue(std::function<void()> task);
46 void cleanup();
47 size_t getThreadCount() const;
48};
A thread pool implementation that manages a pool of worker threads to execute tasks concurrently.
Definition ThreadPool.h:24
bool stop
Definition ThreadPool.h:30
ThreadPool(const ThreadPool &other)=delete
~ThreadPool()
Definition ThreadPool.cpp:32
void enqueue(std::function< void()> task)
Enqueue a new task to be executed by the thread pool.
Definition ThreadPool.cpp:47
std::condition_variable condition
Definition ThreadPool.h:29
size_t getThreadCount() const
Definition ThreadPool.cpp:65
std::vector< std::thread > workers
Definition ThreadPool.h:26
void cleanup()
Definition ThreadPool.cpp:55
ThreadPool(ThreadPool &&other) noexcept=delete
ThreadPool & operator=(const ThreadPool &other)=delete
std::mutex queue_mutex
Definition ThreadPool.h:28
ThreadPool & operator=(ThreadPool &&other) noexcept=delete
std::queue< std::function< void()> > tasks
Definition ThreadPool.h:27