From 4b181e30e32464f0f5757aee5644edabadb238fd Mon Sep 17 00:00:00 2001 From: Micke Prag Date: Wed, 8 Jan 2014 14:59:17 +0100 Subject: [PATCH] Use signals to allow us to exit the application and do proper cleanup --- examples/cpp/callbacks/main.cpp | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/examples/cpp/callbacks/main.cpp b/examples/cpp/callbacks/main.cpp index f3e2d3b1..5afeeaaf 100644 --- a/examples/cpp/callbacks/main.cpp +++ b/examples/cpp/callbacks/main.cpp @@ -1,13 +1,16 @@ #include +#include #include #include +bool running; + class Events { public: Events(); ~Events(); void deviceEvent(int deviceId, int method, const char *data); - + static void deviceEventCallback(int deviceId, int method, const char *data, int callbackId, void *context); private: @@ -43,15 +46,30 @@ void Events::deviceEventCallback(int deviceId, int method, const char *data, int } } +void signalHandler(int sig) { + if (sig == SIGINT) { + printf("Shutting down\n"); + running = false; + } +} + +void run() { + Events ev; + + running = true; + + printf("Listening for events, press control-c to quit...\n"); + //Our own simple eventloop + while(running) { + sleep(1); + } +} + int main(void) { tdInit(); - Events ev; - - //Our own simple eventloop - while(1) { - sleep(100); - } + signal(SIGINT, signalHandler); + run(); tdClose();