From 692a1b7915fa9673e3089427e9d5cb3d02887e0d Mon Sep 17 00:00:00 2001 From: tildearrow Date: Wed, 6 Sep 2023 16:39:35 -0500 Subject: [PATCH] much better --- src/engine/workPool.cpp | 27 +++++++++++++++++++-------- src/engine/workPool.h | 8 ++++---- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/engine/workPool.cpp b/src/engine/workPool.cpp index 6a562bfe2..8772dfba3 100644 --- a/src/engine/workPool.cpp +++ b/src/engine/workPool.cpp @@ -29,7 +29,7 @@ void* _workThread(void* inst) { } void DivWorkThread::run() { - std::unique_lock unique(selfLock); + //std::unique_lock unique(selfLock); DivPendingTask task; bool setFuckingPromise=false; @@ -48,9 +48,12 @@ void DivWorkThread::run() { if (terminate) { break; } - if (notify.wait_for(unique,std::chrono::milliseconds(100))==std::cv_status::timeout) { - logE("this task timed out!"); - } + std::future future=notify.get_future(); + future.wait(); + lock.lock(); + notify=std::promise(); + promiseAlreadySet=false; + lock.unlock(); continue; } else { task=tasks.front(); @@ -94,8 +97,8 @@ bool DivWorkThread::busy() { void DivWorkThread::finish() { lock.lock(); terminate=true; + notify.set_value(); lock.unlock(); - notify.notify_one(); thread->join(); } @@ -133,7 +136,6 @@ void DivWorkPool::wait() { if (!threaded) return; if (busyCount==0) { - logV("nothing to do"); return; } @@ -141,12 +143,21 @@ void DivWorkPool::wait() { // start running for (unsigned int i=0; i #include #include -#include #include #include "fixedQueue.h" @@ -45,12 +44,12 @@ struct DivPendingTask { struct DivWorkThread { DivWorkPool* parent; std::mutex lock; - std::mutex selfLock; std::thread* thread; - std::condition_variable notify; + std::promise notify; FixedQueue tasks; std::atomic isBusy; bool terminate; + bool promiseAlreadySet; void run(); bool assign(const std::function& what, void* arg); @@ -62,7 +61,8 @@ struct DivWorkThread { DivWorkThread(): parent(NULL), isBusy(false), - terminate(false) {} + terminate(false), + promiseAlreadySet(false) {} }; /**