임시저장
This commit is contained in:
@@ -15,17 +15,35 @@ public:
|
||||
~ThreadPool();
|
||||
|
||||
template<typename _Callable, typename... _Args>
|
||||
requires ReturnsVoidPtr<_Callable, _Args...> && (!std::is_same_v<std::decay_t<_Callable>, Thread>)
|
||||
int enqueueJob(_Callable&& __job, void* retVal, _Args&&... __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 = std::bind(std::forward<_Callable>(__job), std::forward<_Args>(__args)...);
|
||||
std::function<void*()> job(std::move(boundFunc));
|
||||
jobs_.push(job, retVal);
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -34,7 +52,7 @@ private:
|
||||
|
||||
std::condition_variable jobQueueCV_;
|
||||
std::mutex jobQueueMutex;
|
||||
std::queue<std::pair<std::function<void*()>, void*>> jobs_;
|
||||
std::queue<std::packaged_task<void()>> jobs_;
|
||||
std::vector<Chattr::Thread> workers_;
|
||||
bool terminate_ = false;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user