blob: c8354e8aa487bf92dff40e6f66333fb98da288f5 [file] [log] [blame]
William A. Kennington IIIfa9431d2018-09-25 14:28:31 -07001/**
2 * A simple example of a repeating timer that prints out a message for
3 * each timer expiration.
4 */
5
6#include <chrono>
7#include <cstdio>
8#include <sdeventplus/clock.hpp>
9#include <sdeventplus/event.hpp>
10#include <sdeventplus/utility/timer.hpp>
11#include <string>
12
13using sdeventplus::Clock;
14using sdeventplus::ClockId;
15using sdeventplus::Event;
16
17constexpr auto clockId = ClockId::RealTime;
18using Timer = sdeventplus::utility::Timer<clockId>;
19
20int main(int argc, char* argv[])
21{
22 if (argc != 2)
23 {
24 fprintf(stderr, "Usage: %s [seconds]\n", argv[0]);
25 return 1;
26 }
27
28 unsigned interval = std::stoul(argv[1]);
29 fprintf(stderr, "Beating every %u seconds\n", interval);
30
31 auto event = Event::get_default();
32 Timer timer(event, [](Timer&) { printf("Beat\n"); },
33 std::chrono::seconds{interval});
34 return event.loop();
35}