Adds a new timer \return Returns the ID of the new timer. You can use this ID to cancel the timer
| 63 | // Returns the ID of the new timer. You can use this ID to cancel the |
| 64 | // timer |
| 65 | uint64_t add(int64_t milliseconds, |
| 66 | std::function<std::pair<bool, int64_t>(bool)> handler) { |
| 67 | WorkItem item; |
| 68 | Clock::time_point tp = Clock::now(); |
| 69 | item.end = tp + std::chrono::milliseconds(milliseconds); |
| 70 | item.period = milliseconds; |
| 71 | item.handler = std::move(handler); |
| 72 | |
| 73 | std::unique_lock<std::mutex> lk(m_mtx); |
| 74 | uint64_t id = ++m_idcounter; |
| 75 | item.id = id; |
| 76 | m_items.push(std::move(item)); |
| 77 | |
| 78 | // Something changed, so wake up timer thread |
| 79 | m_checkWork.notify_one(); |
| 80 | return id; |
| 81 | } |
| 82 | |
| 83 | // Cancels the specified timer |
| 84 | // \return |