4 #include <condition_variable>
25 std::unique_lock<std::mutex> lock(
m_mtx);
32 std::unique_lock<std::mutex> lock(
m_mtx);
37 template <
class Clock,
class Duration>
38 bool waitUntil(
const std::chrono::time_point<Clock, Duration>& point)
40 std::unique_lock<std::mutex> lock(
m_mtx);
41 if (!
m_cv.wait_until(lock, point, [
this]() { return m_count > 0; }))
49 std::condition_variable
m_cv;
64 template <
typename _Clock = std::chrono::steady_clock,
65 typename _Duration = std::chrono::steady_clock::duration>
71 m_th = std::thread([
this] {
run(); });
78 add(std::chrono::milliseconds(0), [
this](
bool) {
m_finish =
true; });
86 uint64_t
add(std::chrono::milliseconds milliseconds, std::function<
void(
bool)> handler)
89 item.end = _Clock::now() + milliseconds;
90 item.handler = std::move(handler);
92 std::unique_lock<std::mutex> lk(
m_mtx);
115 std::unique_lock<std::mutex> lk(
m_mtx);
118 if (item.id ==
id && item.handler)
122 newItem.end = std::chrono::time_point<_Clock, _Duration>();
129 newItem.handler = std::move(item.handler);
130 item.handler =
nullptr;
131 m_items.push(std::move(newItem));
149 std::unique_lock<std::mutex> lk(
m_mtx);
154 item.end = std::chrono::time_point<_Clock, _Duration>();
196 std::pair<bool, std::chrono::time_point<_Clock, _Duration>>
calcWaitTime()
198 std::lock_guard<std::mutex> lk(
m_mtx);
204 return std::make_pair(
true,
m_items.top().end);
215 return std::make_pair(
false, std::chrono::time_point<_Clock, _Duration>());
220 std::unique_lock<std::mutex> lk(
m_mtx);
223 WorkItem item(std::move(
m_items.top()));
228 item.handler(item.id == 0);
240 std::chrono::time_point<_Clock, _Duration>
end;
252 :
public std::priority_queue<WorkItem, std::vector<WorkItem>, std::greater<WorkItem>>