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.

51 lines
1.0 KiB

1 year ago
  1. #include "zworkqueue.hpp"
  2. using namespace iflytop;
  3. using namespace std;
  4. void ZWorkQueue::startSchedule() {
  5. lock_guard<mutex> lock(m_mutex);
  6. m_isJoining = false;
  7. m_workQue.reset(new thread([this]() {
  8. while (true) {
  9. if (m_isJoining) {
  10. break;
  11. }
  12. function<void()> work;
  13. {
  14. lock_guard<mutex> lock(m_QueueMutex);
  15. if (m_workQueue.empty()) {
  16. this_thread::sleep_for(chrono::milliseconds(2));
  17. continue;
  18. }
  19. work = m_workQueue.front();
  20. m_workQueue.pop();
  21. }
  22. if (work) work();
  23. }
  24. }));
  25. workFlag = true;
  26. }
  27. void ZWorkQueue::stopSchedule() {
  28. lock_guard<mutex> lock(m_mutex);
  29. if (m_workQue) {
  30. m_isJoining = true;
  31. m_workQue->join();
  32. m_workQue.reset();
  33. m_workQue = nullptr;
  34. }
  35. workFlag = false;
  36. }
  37. bool ZWorkQueue::push(function<void()> work) {
  38. lock_guard<mutex> lock(m_QueueMutex);
  39. m_workQueue.push(work);
  40. return true;
  41. }
  42. ZWorkQueue::ZWorkQueue() {
  43. // startSchedule();
  44. }
  45. ZWorkQueue::~ZWorkQueue() { stopSchedule(); }