William A. Kennington III | fa9431d | 2018-09-25 14:28:31 -0700 | [diff] [blame^] | 1 | /** |
| 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 | |
| 13 | using sdeventplus::Clock; |
| 14 | using sdeventplus::ClockId; |
| 15 | using sdeventplus::Event; |
| 16 | |
| 17 | constexpr auto clockId = ClockId::RealTime; |
| 18 | using Timer = sdeventplus::utility::Timer<clockId>; |
| 19 | |
| 20 | int 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 | } |