2021-10-12 14:20:30 +02:00
|
|
|
#pragma once
|
|
|
|
|
#include <thread>
|
2024-01-20 19:14:19 +01:00
|
|
|
#include <functional>
|
2023-12-17 16:16:46 +01:00
|
|
|
#include "Containers/Array.h"
|
2021-10-23 00:22:35 +02:00
|
|
|
#include "Containers/List.h"
|
2021-10-12 14:20:30 +02:00
|
|
|
|
|
|
|
|
namespace Seele
|
|
|
|
|
{
|
|
|
|
|
class ThreadPool
|
|
|
|
|
{
|
|
|
|
|
public:
|
2023-12-17 16:16:46 +01:00
|
|
|
ThreadPool(uint32 numWorkers = std::thread::hardware_concurrency());
|
|
|
|
|
~ThreadPool();
|
|
|
|
|
void runAndWait(List<std::function<void()>> functions);
|
2021-10-12 14:20:30 +02:00
|
|
|
private:
|
2023-12-17 16:16:46 +01:00
|
|
|
struct Task
|
|
|
|
|
{
|
|
|
|
|
uint64 numRemaining = 0;
|
|
|
|
|
List<std::function<void()>> functions;
|
|
|
|
|
};
|
|
|
|
|
void work();
|
2022-03-19 22:45:30 +01:00
|
|
|
Array<std::thread> workers;
|
2023-12-17 16:16:46 +01:00
|
|
|
std::mutex taskLock;
|
|
|
|
|
std::condition_variable taskCV;
|
|
|
|
|
std::condition_variable completedCV;
|
|
|
|
|
Task currentTask;
|
|
|
|
|
bool running = true;
|
2021-12-15 00:05:42 +01:00
|
|
|
};
|
2023-12-17 16:16:46 +01:00
|
|
|
}
|