62 lines
1.7 KiB
C++
62 lines
1.7 KiB
C++
#pragma once
|
|
#include "Thread.hpp"
|
|
#include <vector>
|
|
#include <queue>
|
|
#include <functional>
|
|
#include <condition_variable>
|
|
#include <mutex>
|
|
#include <memory>
|
|
|
|
namespace Chattr {
|
|
|
|
class ThreadPool {
|
|
public:
|
|
ThreadPool(std::uint32_t numThreads);
|
|
~ThreadPool();
|
|
|
|
template<typename _Callable, typename... _Args>
|
|
requires (!std::is_void_v<std::invoke_result_t<_Callable, _Args...>>)
|
|
int enqueueJob(_Callable&& __job, std::invoke_result_t<_Callable, _Args...>& retVal, _Args&&... __args) {
|
|
if (terminate_) {
|
|
spdlog::error("Cannot run jobs on threads that terminating...");
|
|
return -1;
|
|
}
|
|
|
|
std::lock_guard<std::mutex> lock(jobQueueMutex);
|
|
auto boundFunc = [&retVal, __job = std::move(__job), ... __args = std::move(__args)]() mutable {
|
|
retVal = __job(std::move(__args)...);
|
|
};
|
|
auto task = std::packaged_task<void()>(std::move(boundFunc));
|
|
jobs_.push(std::move(task));
|
|
jobQueueCV_.notify_one();
|
|
}
|
|
template<typename _Callable, typename... _Args>
|
|
requires std::is_void_v<std::invoke_result_t<_Callable, _Args...>>
|
|
int enqueueJob(_Callable&& __job, _Args&&... __args) {
|
|
if (terminate_) {
|
|
spdlog::error("Cannot run jobs on threads that terminating...");
|
|
return -1;
|
|
}
|
|
|
|
std::lock_guard<std::mutex> lock(jobQueueMutex);
|
|
auto boundFunc = [__job = std::move(__job), ... __args = std::move(__args)]() mutable {
|
|
__job(std::move(__args)...);
|
|
};
|
|
auto task = std::packaged_task<void()>(std::move(boundFunc));
|
|
jobs_.push(std::move(task));
|
|
jobQueueCV_.notify_one();
|
|
|
|
return 0;
|
|
}
|
|
|
|
private:
|
|
void* Worker();
|
|
|
|
std::condition_variable jobQueueCV_;
|
|
std::mutex jobQueueMutex;
|
|
std::queue<std::packaged_task<void()>> jobs_;
|
|
std::vector<Chattr::Thread> workers_;
|
|
bool terminate_ = false;
|
|
};
|
|
|
|
} |