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 <functional> 9 #include <sdeventplus/clock.hpp> 10 #include <sdeventplus/event.hpp> 11 #include <sdeventplus/source/signal.hpp> 12 #include <sdeventplus/utility/timer.hpp> 13 #include <stdplus/signal.hpp> 14 #include <string> 15 16 using sdeventplus::Clock; 17 using sdeventplus::ClockId; 18 using sdeventplus::Event; 19 using sdeventplus::source::Signal; 20 21 constexpr auto clockId = ClockId::RealTime; 22 using Timer = sdeventplus::utility::Timer<clockId>; 23 24 void intCb(Signal& signal, const struct signalfd_siginfo*) 25 { 26 printf("Exiting\n"); 27 signal.get_event().exit(0); 28 } 29 30 int main(int argc, char* argv[]) 31 { 32 if (argc != 2) 33 { 34 fprintf(stderr, "Usage: %s [seconds]\n", argv[0]); 35 return 1; 36 } 37 38 unsigned interval = std::stoul(argv[1]); 39 fprintf(stderr, "Beating every %u seconds\n", interval); 40 41 auto event = Event::get_default(); 42 Timer timer( 43 event, [](Timer&) { printf("Beat\n"); }, 44 std::chrono::seconds{interval}); 45 stdplus::signal::block(SIGINT); 46 Signal(event, SIGINT, intCb).set_floating(true); 47 return event.loop(); 48 } 49