| 156 | */ |
| 157 | template<class X, class Y> |
| 158 | void delay(task_id_t task_id, std::chrono::duration<X, Y> duration) { |
| 159 | std::lock_guard<std::mutex> lg(_task_mutex); |
| 160 | |
| 161 | auto it = _timer_tasks.begin(); |
| 162 | for (; it < _timer_tasks.cend(); ++it) { |
| 163 | const __task &task = std::get<1>(*it); |
| 164 | |
| 165 | if (&*task == task_id) { |
| 166 | std::get<0>(*it) = std::chrono::steady_clock::now() + duration; |
| 167 | |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (it == _timer_tasks.cend()) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | // smaller time goes to the back |
| 177 | auto prev = it - 1; |
| 178 | while (it > _timer_tasks.cbegin()) { |
| 179 | if (std::get<0>(*it) > std::get<0>(*prev)) { |
| 180 | std::swap(*it, *prev); |
| 181 | } |
| 182 | |
| 183 | --prev; |
| 184 | --it; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | bool cancel(task_id_t task_id) { |
| 189 | std::lock_guard lg(_task_mutex); |