diff options
| author | Stefan Boberg <[email protected]> | 2026-02-23 09:53:21 +0100 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2026-02-23 09:53:21 +0100 |
| commit | 8a8a50d1648ebb1fed1a9fdfc1113f185fd1b20d (patch) | |
| tree | 95789dba923ad3d7fa0ca2d758b4f5fbdf327a20 /src/zencore/include | |
| parent | add friends to fix build issue (diff) | |
| download | zen-8a8a50d1648ebb1fed1a9fdfc1113f185fd1b20d.tar.xz zen-8a8a50d1648ebb1fed1a9fdfc1113f185fd1b20d.zip | |
implemented dynamic scaling support for WorkerThreadPool
Diffstat (limited to 'src/zencore/include')
| -rw-r--r-- | src/zencore/include/zencore/blockingqueue.h | 27 | ||||
| -rw-r--r-- | src/zencore/include/zencore/workthreadpool.h | 5 |
2 files changed, 30 insertions, 2 deletions
diff --git a/src/zencore/include/zencore/blockingqueue.h b/src/zencore/include/zencore/blockingqueue.h index e91fdc659..d205b6ec3 100644 --- a/src/zencore/include/zencore/blockingqueue.h +++ b/src/zencore/include/zencore/blockingqueue.h @@ -3,6 +3,7 @@ #pragma once #include <atomic> +#include <chrono> #include <condition_variable> #include <deque> #include <mutex> @@ -48,6 +49,32 @@ public: return true; } + // Returns: true if item dequeued, false on timeout or completion + template<typename Rep, typename Period> + bool WaitAndDequeueFor(T& Item, std::chrono::duration<Rep, Period> Timeout) + { + std::unique_lock Lock(m_Lock); + if (m_Queue.empty()) + { + if (m_CompleteAdding) + { + return false; + } + if (!m_NewItemSignal.wait_for(Lock, Timeout, [this]() { return !m_Queue.empty() || m_CompleteAdding; })) + { + return false; // Timed out + } + if (m_Queue.empty()) + { + ZEN_ASSERT(m_CompleteAdding); + return false; + } + } + Item = std::move(m_Queue.front()); + m_Queue.pop_front(); + return true; + } + void CompleteAdding() { std::unique_lock Lock(m_Lock); diff --git a/src/zencore/include/zencore/workthreadpool.h b/src/zencore/include/zencore/workthreadpool.h index 932e3d404..cb0b8f491 100644 --- a/src/zencore/include/zencore/workthreadpool.h +++ b/src/zencore/include/zencore/workthreadpool.h @@ -27,8 +27,9 @@ private: class WorkerThreadPool { public: - explicit WorkerThreadPool(int InThreadCount, bool UseExplicitThreads = false); - WorkerThreadPool(int InThreadCount, std::string_view WorkerThreadBaseName, bool UseExplicitThreads = false); + explicit WorkerThreadPool(int InThreadCount, bool UseExplicitThreads = true); + WorkerThreadPool(int InThreadCount, std::string_view WorkerThreadBaseName, bool UseExplicitThreads = true); + WorkerThreadPool(int InMinThreadCount, int InMaxThreadCount, std::string_view WorkerThreadBaseName, bool UseExplicitThreads = true); ~WorkerThreadPool(); // Decides what to do if there are no free workers in the pool when the work is submitted |