From 2584f9d7b91e2d14ddc9bdcb45fc5133a083b522 Mon Sep 17 00:00:00 2001 From: "William A. Kennington III" Date: Sat, 23 Mar 2019 21:05:48 -0700 Subject: example/heartbeat: Add signal handling This makes the example a little more interesting as we now have multiple sources and we can analyze memory safety with valgrind by just sending a SIGINT to quit cleanly. Change-Id: Id15fe58f798d3b137f91228025f48a5e4cd5fc50 Signed-off-by: William A. Kennington III --- example/heartbeat_timer.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'example/heartbeat_timer.cpp') diff --git a/example/heartbeat_timer.cpp b/example/heartbeat_timer.cpp index c8354e8..63c33c0 100644 --- a/example/heartbeat_timer.cpp +++ b/example/heartbeat_timer.cpp @@ -5,18 +5,28 @@ #include #include +#include #include #include +#include #include +#include #include using sdeventplus::Clock; using sdeventplus::ClockId; using sdeventplus::Event; +using sdeventplus::source::Signal; constexpr auto clockId = ClockId::RealTime; using Timer = sdeventplus::utility::Timer; +void intCb(Signal& signal, const struct signalfd_siginfo*) +{ + printf("Exiting\n"); + signal.get_event().exit(0); +} + int main(int argc, char* argv[]) { if (argc != 2) @@ -31,5 +41,7 @@ int main(int argc, char* argv[]) auto event = Event::get_default(); Timer timer(event, [](Timer&) { printf("Beat\n"); }, std::chrono::seconds{interval}); + stdplus::signal::block(SIGINT); + Signal signal(event, SIGINT, intCb); return event.loop(); } -- cgit v1.2.3