diff options
| author | William A. Kennington III <wak@google.com> | 2018-09-25 14:28:31 -0700 |
|---|---|---|
| committer | William A. Kennington III <wak@google.com> | 2018-09-26 12:18:50 -0700 |
| commit | fa9431d52f37bf51a2aa0d3a85eaaf027db8488f (patch) | |
| tree | c90245c7f7f03a8dffcd93d710a989c69960d534 /example/heartbeat_timer.cpp | |
| parent | 406b86e47e982d4ae5d9fa9f39476cb81f41d449 (diff) | |
| download | sdeventplus-fa9431d52f37bf51a2aa0d3a85eaaf027db8488f.tar.gz sdeventplus-fa9431d52f37bf51a2aa0d3a85eaaf027db8488f.zip | |
example: Add a repeating timer sample
Tested:
Ran through unit test suite and manually executed the example
program to make sure it works as expected.
Change-Id: I77cffdd038df4eab774f0d162f49273650638ad6
Signed-off-by: William A. Kennington III <wak@google.com>
Diffstat (limited to 'example/heartbeat_timer.cpp')
| -rw-r--r-- | example/heartbeat_timer.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/example/heartbeat_timer.cpp b/example/heartbeat_timer.cpp new file mode 100644 index 0000000..c8354e8 --- /dev/null +++ b/example/heartbeat_timer.cpp @@ -0,0 +1,35 @@ +/** + * A simple example of a repeating timer that prints out a message for + * each timer expiration. + */ + +#include <chrono> +#include <cstdio> +#include <sdeventplus/clock.hpp> +#include <sdeventplus/event.hpp> +#include <sdeventplus/utility/timer.hpp> +#include <string> + +using sdeventplus::Clock; +using sdeventplus::ClockId; +using sdeventplus::Event; + +constexpr auto clockId = ClockId::RealTime; +using Timer = sdeventplus::utility::Timer<clockId>; + +int main(int argc, char* argv[]) +{ + if (argc != 2) + { + fprintf(stderr, "Usage: %s [seconds]\n", argv[0]); + return 1; + } + + unsigned interval = std::stoul(argv[1]); + fprintf(stderr, "Beating every %u seconds\n", interval); + + auto event = Event::get_default(); + Timer timer(event, [](Timer&) { printf("Beat\n"); }, + std::chrono::seconds{interval}); + return event.loop(); +} |

