#include "zworkqueue.hpp" using namespace iflytop; using namespace std; void ZWorkQueue::startSchedule() { lock_guard lock(m_mutex); m_isJoining = false; m_workQue.reset(new thread([this]() { while (true) { if (m_isJoining) { break; } function work; { lock_guard lock(m_QueueMutex); if (m_workQueue.empty()) { this_thread::sleep_for(chrono::milliseconds(2)); continue; } work = m_workQueue.front(); m_workQueue.pop(); } if (work) work(); } })); workFlag = true; } void ZWorkQueue::stopSchedule() { lock_guard lock(m_mutex); if (m_workQue) { m_isJoining = true; m_workQue->join(); m_workQue.reset(); m_workQue = nullptr; } workFlag = false; } bool ZWorkQueue::push(function work) { lock_guard lock(m_QueueMutex); m_workQueue.push(work); return true; } ZWorkQueue::ZWorkQueue() { // startSchedule(); } ZWorkQueue::~ZWorkQueue() { stopSchedule(); }