Files
RayTracer/src/ThreadPool.h
T

28 lines
587 B
C++
Raw Normal View History

2025-01-24 18:21:34 +01:00
#pragma once
#include <condition_variable>
#include <functional>
#include <list>
#include <thread>
2025-01-25 13:28:52 +01:00
#include "Minimal.h"
2025-01-24 18:21:34 +01:00
2025-01-24 21:54:38 +01:00
struct Batch
{
2025-01-25 13:28:52 +01:00
std::list<Task> jobs;
2025-01-24 21:54:38 +01:00
};
2025-01-24 18:21:34 +01:00
class ThreadPool
{
public:
ThreadPool(uint32_t numWorkers = std::thread::hardware_concurrency());
~ThreadPool();
2025-01-24 21:54:38 +01:00
void runBatch(Batch&& batch);
2025-01-24 18:21:34 +01:00
private:
std::atomic_bool running = true;
void work();
std::mutex queueLock;
std::condition_variable queueCV;
std::condition_variable completedCV;
2025-01-25 13:28:52 +01:00
uint32_t numRemaining;
2025-01-24 21:54:38 +01:00
std::list<Batch> taskQueue;
2025-01-24 18:21:34 +01:00
std::vector<std::thread> workers;
};