Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
ThreadPool.hpp
Go to the documentation of this file.
1#pragma once
3#include <vector>
4#include <thread>
5#include <queue>
6#include <mutex>
7#include <condition_variable>
8#include <functional>
9#include <atomic>
10
11namespace orbit::concurrency {
12
14public:
15 // Create a thread pool with a specific number of threads
16 explicit ThreadPool(size_t num_threads);
17
18 // Join all threads and shut down the pool safely
20
21 ThreadPool(const ThreadPool&) = delete;
22 ThreadPool& operator=(const ThreadPool&) = delete;
23
24 // Add a new task to the queue for a worker thread to pick up
25 void enqueue(std::function<void()> task);
26
27private:
29
30 std::vector<std::thread> workers_;
31 std::queue<std::function<void()>> tasks_;
32
33 // Synchronization primitives
34 std::mutex queue_mutex_;
35 std::condition_variable condition_;
36
37 // Atomic flag to signal threads to stop processing and exit
38 std::atomic<bool> stop_{false};
39};
40
41} // namespace concurrency
Definition ThreadPool.hpp:13
ThreadPool(const ThreadPool &)=delete
std::mutex queue_mutex_
Definition ThreadPool.hpp:34
void enqueue(std::function< void()> task)
std::condition_variable condition_
Definition ThreadPool.hpp:35
std::atomic< bool > stop_
Definition ThreadPool.hpp:38
ThreadPool(size_t num_threads)
std::vector< std::thread > workers_
Definition ThreadPool.hpp:30
ThreadPool & operator=(const ThreadPool &)=delete
std::queue< std::function< void()> > tasks_
Definition ThreadPool.hpp:31
Definition Awaitable.hpp:10