30 void Worker::startWorking()
33 std::unique_lock<std::mutex> l(x_work);
37 m_state.compare_exchange_strong(ex, WorkerState::Starting);
38 m_state_notifier.notify_all();
42 m_state = WorkerState::Starting;
43 m_state_notifier.notify_all();
44 m_work.reset(
new thread([&]()
48 while (m_state != WorkerState::Killing)
53 unique_lock<mutex> l(x_work);
54 m_state = WorkerState::Started;
57 m_state_notifier.notify_all();
65 catch (std::exception
const& _e)
67 cwarn <<
"Exception thrown in Worker thread: " << _e.what();
75 unique_lock<mutex> l(x_work);
76 ex = m_state.exchange(WorkerState::Stopped);
78 if (ex == WorkerState::Killing || ex == WorkerState::Starting)
81 m_state_notifier.notify_all();
85 unique_lock<mutex> l(x_work);
87 while (m_state == WorkerState::Stopped)
88 m_state_notifier.wait(l);
96 while (m_state == WorkerState::Starting)
97 m_state_notifier.wait(l);
100 void Worker::stopWorking()
102 std::unique_lock<Mutex> l(x_work);
106 if (!m_state.compare_exchange_strong(ex, WorkerState::Stopping))
108 m_state_notifier.notify_all();
111 while (m_state != WorkerState::Stopped)
112 m_state_notifier.wait(l);
116 void Worker::terminate()
119 std::unique_lock<Mutex> l(x_work);
122 if (m_state.exchange(WorkerState::Killing) == WorkerState::Killing)
125 m_state_notifier.notify_all();
134 void Worker::workLoop()
136 while (m_state == WorkerState::Started)
139 this_thread::sleep_for(chrono::milliseconds(m_idleWaitMs));