diff --git a/include/stdex/watchdog.hpp b/include/stdex/watchdog.hpp index 67558d9dc..e5b87032e 100644 --- a/include/stdex/watchdog.hpp +++ b/include/stdex/watchdog.hpp @@ -66,17 +66,27 @@ namespace stdex protected: void run() { + size_t phase; for (;;) { - std::unique_lock lk(m_mutex); - auto phase = m_phase; - if (m_cv.wait_for(lk, m_timeout, [&] {return m_quit || phase != m_phase; })) { + { + std::unique_lock lk(m_mutex); + phase = m_phase; + if (m_cv.wait_for(lk, m_timeout, [&] {return m_quit || phase != m_phase; })) { + if (m_quit) + break; + // reset() called in time. + continue; + } + } + // Timeout + m_callback(); + { + // Sleep until next reset(). + std::unique_lock lk(m_mutex); + m_cv.wait(lk, [&] {return m_quit || phase != m_phase; }); if (m_quit) break; } - else { - m_callback(); - break; - } } }