diff --git a/src/engine/helper/HL_ThreadPool.h b/src/engine/helper/HL_ThreadPool.h index ddde688a..a374b5de 100644 --- a/src/engine/helper/HL_ThreadPool.h +++ b/src/engine/helper/HL_ThreadPool.h @@ -11,13 +11,15 @@ #include #include -class ThreadPool { +class ThreadPool +{ public: ThreadPool(size_t); template auto enqueue(F&& f, Args&&... args) -> std::future::type>; ~ThreadPool(); + void setPause(bool v) { this->pause = v;} private: // need to keep track of threads so we can join them std::vector< std::thread > workers; @@ -27,12 +29,12 @@ private: // synchronization std::mutex queue_mutex; std::condition_variable condition; - bool stop; + bool stop, pause; }; // the constructor just launches some amount of workers inline ThreadPool::ThreadPool(size_t threads) - : stop(false) + : stop(false), pause(false) { for(size_t i = 0;i lock(this->queue_mutex); this->condition.wait(lock, - [this]{ return this->stop || !this->tasks.empty(); }); + [this]{ return this->stop || !this->pause || !this->tasks.empty(); }); + if (this->tasks.empty()) + std::this_thread::sleep_for(std::chrono::milliseconds(10)); if(this->stop && this->tasks.empty()) return; + if(this->pause) + continue; task = std::move(this->tasks.front()); this->tasks.pop(); }