-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperiodic_timer_example.cpp
More file actions
79 lines (66 loc) · 2.58 KB
/
Copy pathperiodic_timer_example.cpp
File metadata and controls
79 lines (66 loc) · 2.58 KB
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
/** @file
*
* @brief Example code demonstrating use of @c chops::periodic_timer
*
* @author Thurman Gillespy
*
* @copyright (c) 2019 by Thurman Gillespy
* 3/23/19
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
* Sample make file:
* g++ -std=c++17 -I ~/Projects/utility-rack/include/ \
* -I ~/Projects/asio/asio/include/ \
* -I ~/Projects/ timer_demo.cpp -lpthread
*
* This also builds with CMake files with C++ 20 specified
*/
#include <iostream>
#include <cstdlib> // EXIT_SUCCESS
#include <chrono> // std::chrono
#include <thread> // std::thread
#include "asio/executor_work_guard.hpp"
#include "asio/executor.hpp"
#include "timer/periodic_timer.hpp"
/**
* @brief The @c chops::periodic_timer calls a user supplied callback
* function at a user defined interval. The callback funtion is repeatedly
* called while it returns @c true, but the timer terminates when the
* function returns @c false.
*
* The timer executes inside an @c std::thread. The thread and timer are linked
* with an @c asio::io_context. As the example shows, multiple timers can be
* run inside one thread. An @c asio::executor_work_guard controls ownership
* of executor work within the thread. Without either the thread or the @c
* exector_work_guard, the timer will not execute.
*/
int main() {
using Clock = std::chrono::steady_clock;
using wk_guard = asio::executor_work_guard<asio::io_context::executor_type>;
asio::io_context ioc;
chops::periodic_timer timer1 {ioc};
chops::periodic_timer timer2 {ioc};
wk_guard wg { asio::make_work_guard(ioc) };
std::thread thr([&ioc] () { ioc.run(); } );
constexpr auto dur1 { 500 };
constexpr auto dur2 { 500 };
constexpr int num_repeats = 5;
// lambda functions called by timer every dur msec
auto f1 = [&num_repeats] (std::error_code err, Clock::duration elap) {
static int i = 0; std::cout << "Hello, "; std::cout.flush();
return (++i >= num_repeats ? false : true); };
auto f2 = [&num_repeats] (std::error_code err, Clock::duration elap) {
static int i = 0; std::cout << "World! "; std::cout.flush();
return (++i >= num_repeats ? false : true); };
// call f every dur msec
timer1.start_duration_timer(std::chrono::milliseconds(dur1), f1);
timer2.start_duration_timer(std::chrono::milliseconds(dur2), f2);
// release work_guard
wg.reset();
// reattach thread
thr.join();
std::cout << "\ntimer finished\n";
return EXIT_SUCCESS;
}