HPC_Voxel_Engine 0.2.0
High-Performance C++ Voxel Engine
Loading...
Searching...
No Matches
ThreadPool.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <functional>
5#include <iostream>
6#include <thread>
7#include <vector>
8#include "ThreadSafeQueue.h"
9
10namespace Core {
11
18public:
23 ThreadPool() : m_bDone(false) {
24 // Calculate optimal thread count
25 unsigned int iThreadCt = std::thread::hardware_concurrency();
26 if (iThreadCt > 1)
27 iThreadCt -= 1; // Leave one for main thread
28 else
29 iThreadCt = 1; // Fallback for single-core
30
31 // Launch workers
32 m_objJThreads.reserve(iThreadCt);
33 for (unsigned int iCt = 0; iCt < iThreadCt; iCt++) {
34 // jthread passes a stop_token automatically
35 m_objJThreads.emplace_back([this](std::stop_token st) { worker_loop(st); });
36 }
37 }
38
43 m_bDone = true;
44
45 // 1. Wake up all threads waiting on the queue
47
48 // 2. Request stop on jthreads (sets stop_token)
49 for (auto& objThread : m_objJThreads) {
50 objThread.request_stop();
51 }
52
53 // 3. jthreads automatically join here on destruction
54 }
55
59 void submit(std::function<void()> objJob) {
60 if (!m_bDone) {
61 m_ObjQueue.push(std::move(objJob));
62 }
63 }
64
65private:
70 void worker_loop(std::stop_token st) {
71 while (!st.stop_requested() && !m_bDone) {
72 std::function<void()> objJob;
73
74 // wait_and_pop blocks until job is available or queue is invalidated
75 if (m_ObjQueue.wait_and_pop(objJob)) {
76 if (objJob) {
77 objJob();
78 }
79 } else {
80 // If wait_and_pop returns false, queue was invalidated (shutdown)
81 break;
82 }
83 }
84 }
85
86 std::vector<std::jthread> m_objJThreads;
87 std::atomic<bool> m_bDone;
88 ThreadSafeQueue<std::function<void()>> m_ObjQueue;
89};
90
91} // namespace Core
Manages a pool of worker threads that consume tasks from a ThreadSafeQueue. Utilizes C++20 std::jthre...
Definition ThreadPool.h:17
void submit(std::function< void()> objJob)
Submits a void() function/lambda to the pool.
Definition ThreadPool.h:59
std::atomic< bool > m_bDone
Definition ThreadPool.h:87
ThreadSafeQueue< std::function< void()> > m_ObjQueue
Definition ThreadPool.h:88
ThreadPool()
Constructs the thread pool. Defaults to (Hardware Threads - 1) to leave the main thread free.
Definition ThreadPool.h:23
std::vector< std::jthread > m_objJThreads
Definition ThreadPool.h:86
void worker_loop(std::stop_token st)
The main loop for worker threads.
Definition ThreadPool.h:70
~ThreadPool()
Destructor. Stops all threads and joins them.
Definition ThreadPool.h:42
A thread-safe FIFO queue wrapper using mutexes and condition variables. Designed for producer-consume...
Definition ThreadSafeQueue.h:17
void invalidate()
Wakes up all waiting threads effectively cancelling the wait.
Definition ThreadSafeQueue.h:88
bool wait_and_pop(T &objValue)
Waits until the queue is not empty, then pops the front element.
Definition ThreadSafeQueue.h:50
void push(T &&objValue)
Pushes a value into the queue (Move semantics).
Definition ThreadSafeQueue.h:26
Definition camera.h:6