-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtimer.cpp
More file actions
24 lines (22 loc) · 713 Bytes
/
timer.cpp
File metadata and controls
24 lines (22 loc) · 713 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "timer.hpp"
#include <thread>
Timer::Timer() {
}
void Timer::add(std::chrono::milliseconds delay,
std::function<void()> callback,
bool asynchronous) {
if (asynchronous) {
std::thread([=]() {
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
callback();
}).detach();
}
else {
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
callback();
}
}
// USING: Timer timer;
// USING: timer.add(std::chrono::milliseconds(1000), bar, true);
// USING: timer.add(std::chrono::milliseconds(500), foo);
// USING: timer.add(std::chrono::milliseconds(2000), []{}, false);