The predicate in the second cv.wait_for is incorrect:
m_cv.wait_for(lock, m_queue.top()->get_timepoint() - now, [&]() { return is_disposed() || !m_queue.is_empty() || m_queue.top()->get_timepoint() <= worker_strategy::now(); });
!m_queue.is_empty() is "always" true at this point, because we’re already in a scope where that condition has been checked. As a result, the predicate immediately evaluates to true, making the wait return right away and causing a busy loop. This leads to high CPU usage when using interval operators combined with run loops.
Fix suggestion, change predicate to:
m_cv.wait_for(lock, m_queue.top()->get_timepoint() - now, [&]() { return is_disposed() || m_queue.is_empty() || m_queue.top()->get_timepoint() <= worker_strategy::now(); });
The predicate in the second cv.wait_for is incorrect:
m_cv.wait_for(lock, m_queue.top()->get_timepoint() - now, [&]() { return is_disposed() || !m_queue.is_empty() || m_queue.top()->get_timepoint() <= worker_strategy::now(); });!m_queue.is_empty()is "always" true at this point, because we’re already in a scope where that condition has been checked. As a result, the predicate immediately evaluates to true, making the wait return right away and causing a busy loop. This leads to high CPU usage when using interval operators combined with run loops.Fix suggestion, change predicate to:
m_cv.wait_for(lock, m_queue.top()->get_timepoint() - now, [&]() { return is_disposed() || m_queue.is_empty() || m_queue.top()->get_timepoint() <= worker_strategy::now(); });