You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

52 lines
1.0 KiB

#include "zworkqueue.hpp"
using namespace iflytop;
using namespace std;
void ZWorkQueue::startSchedule() {
lock_guard<mutex> lock(m_mutex);
m_isJoining = false;
m_workQue.reset(new thread([this]() {
while (true) {
if (m_isJoining) {
break;
}
function<void()> work;
{
lock_guard<mutex> 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<mutex> lock(m_mutex);
if (m_workQue) {
m_isJoining = true;
m_workQue->join();
m_workQue.reset();
m_workQue = nullptr;
}
workFlag = false;
}
bool ZWorkQueue::push(function<void()> work) {
lock_guard<mutex> lock(m_QueueMutex);
m_workQueue.push(work);
return true;
}
ZWorkQueue::ZWorkQueue() {
// startSchedule();
}
ZWorkQueue::~ZWorkQueue() { stopSchedule(); }