#include "WorkerThread.hpp" #include #include //#define MNN_OPEN_TIME_TRACE #include using namespace std; namespace MNN { WorkerThread::WorkerThread(int numberThread) { for (int i=0; i _l(mQueueMutex); mCondition.wait(_l, [this] { return mStop || mTasks.size() > 0;}); if (mTasks.empty()) { continue; } f = mTasks.front(); mTasks.pop(); } f->content(); delete f; } }); } } WorkerThread::~WorkerThread() { { std::lock_guard _l(mQueueMutex); mStop = true; } mCondition.notify_all(); for (auto& worker : mWorkers) { worker.join(); } // Complete Remain work while (!mTasks.empty()) { auto f = mTasks.front(); f->content(); mTasks.pop(); delete f; } } bool WorkerThread::postTask(std::function&& task) { { AUTOTIME; std::unique_lock _l(mQueueMutex); auto taskWrap = new Task; taskWrap->content = std::move(task); mTasks.push(taskWrap); } mCondition.notify_all(); return true; } } // namespace MNN