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