-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtimer.h
81 lines (71 loc) · 2.24 KB
/
timer.h
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
/* dns64perf++ - C++14 DNS64 performance tester
* Based on dns64perf by Gabor Lencse <[email protected]>
* (http://ipv6.tilb.sze.hu/dns64perf/)
* Copyright (C) 2017 Daniel Bakai <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
/** @file
* @brief Header for a generic Timer class
*/
#ifndef TIMER_H_INCLUDED
#define TIMER_H_INCLUDED
#include <atomic>
#include <chrono>
#include <functional>
#include <thread>
/**
* Class to represent a generic, function execution time corrected timer.
*/
class Timer {
private:
std::string thread_name_;
std::function<void(void)>
prepare_; /**< function to run once vefore repeating the task */
std::function<void(void)>
task_; /**< std::function polymorphic template to store the task */
std::chrono::nanoseconds interval_; /**< Timer interval in nanoseconds */
size_t n_; /**< Number of times to repeat */
std::thread thread_; /**< The thread on which the timer executes */
std::atomic<bool> stop_; /**< Atomic variable to stop the timer */
/**
* Function to execute on the thread
*/
void run();
public:
/**
* Constructor.
* @param task task to execute
* @param interval timer interval in nanoseconds
* @param n number of time to repeat
*/
Timer(const std::string &thread_name, std::function<void(void)> &&prepare,
std::function<void(void)> &&task, std::chrono::nanoseconds interval,
size_t n);
/**
* Destructor.
*/
~Timer();
/**
* Starts timer.
*/
void start();
/**
* Stops timer.
*/
void stop();
};
#endif