-
Notifications
You must be signed in to change notification settings - Fork 0
/
04-chrono.cc
executable file
·153 lines (127 loc) · 3.16 KB
/
04-chrono.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// Program
// A class with simulates the functionality timer. On expiration
// of time, timer can execute a callback function too.
// Timer can configured in different ways:
// - It can be asked to run for specific number of times.
// - or it can be asked to run for infinite number of times.
// - Use stop function to stop this timer.
//
// Compile
// g++ -Wall -Wextra -pedantic -std=c++17 -pthread -o 04-chrono 04-chrono.cc
//
// Execution
// ./04-chrono
//
#include <iostream>
#include <thread>
#include <functional>
#include <chrono>
#include <atomic>
class Timer {
private:
intmax_t time_out_ms_;
intmax_t shot_count_;
std::function<void(void)> callback_;
std::atomic<bool> is_running_;
std::thread thread_;
void thread_callback() {
intmax_t times = 0;
while (is_running_ == true && (shot_count_ == 0 || times < shot_count_)) {
if (callback_ != nullptr) {
callback_();
}
std::this_thread::sleep_for(std::chrono::milliseconds(time_out_ms_));
++times;
}
is_running_ = false;
}
public:
Timer() = delete;
Timer(const Timer&) = delete;
Timer& operator=(const Timer&) = delete;
Timer(Timer &&) = delete;
explicit Timer(intmax_t time_out_ms, intmax_t shot_count=1,
std::function<void(void)> callback=nullptr):
time_out_ms_(time_out_ms),
shot_count_(shot_count),
callback_(callback),
is_running_(false),
thread_({}) {
}
void start() {
is_running_ = true;
thread_ = std::thread(&Timer::thread_callback, this);
}
// It is blocking call, as it wait
// until underlying thread is not finished
void stop() {
try {
// Ask thread to stop execution
is_running_ = false;
// Do busy wait until thread is not being marked as dead
if (thread_.joinable()) {
thread_.join();
}
}
catch(...) {}
}
bool is_active() const {
return this->is_running_.load();
}
intmax_t get_time_out(void) const {
return this->time_out_ms_;
}
intmax_t get_shot_count(void) const {
return this->shot_count_;
}
~Timer() {
stop();
}
};
// Timer callback
static uint64_t timer_callback_counter = 0;
void callback() {
std::cout << "Timer callback is called: " << ++timer_callback_counter << '\n';
}
//
// Entry function
//
int main() {
{
std::thread t([] {
// Reset callback counter
timer_callback_counter = 0;
// Create timer for 5sec for 150
Timer myTimer(5, 25, callback);
std::cout << "Start timer..." << '\n';
myTimer.start();
while (myTimer.is_active())
{
}
std::cout << "Timer is stopped" << '\n';
myTimer.stop();
});
t.join();
}
{
std::thread t([] {
// Reset callback counter
timer_callback_counter = 0;
// Keep timer running
Timer myTimer(6, 0, callback);
std::cout << "Start timer..." << '\n';
myTimer.start();
while (myTimer.is_active())
{
// After a while ask thread to shutdown itself
if (timer_callback_counter >= 500) {
std::cout << "Timer is stopped" << '\n';
myTimer.stop();
}
}
});
t.join();
}
return 0;
}