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>
diff --git a/.gitignore b/.gitignore
index b91a4df..d9423e4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -43,6 +43,7 @@
# Output binaries
/example/follow
/example/heartbeat
+/example/heartbeat_timer
/test/clock
/test/event
/test/exception
diff --git a/example/Makefile.am b/example/Makefile.am
index 7d4be73..96f2faa 100644
--- a/example/Makefile.am
+++ b/example/Makefile.am
@@ -10,4 +10,8 @@
heartbeat_SOURCES = heartbeat.cpp
heartbeat_LDADD = $(SDEVENTPLUS_LIBS)
+noinst_PROGRAMS += heartbeat_timer
+heartbeat_timer_SOURCES = heartbeat_timer.cpp
+heartbeat_timer_LDADD = $(SDEVENTPLUS_LIBS)
+
endif
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();
+}
diff --git a/src/sdeventplus/utility/timer.hpp b/src/sdeventplus/utility/timer.hpp
index 8865ba1..96c5b6b 100644
--- a/src/sdeventplus/utility/timer.hpp
+++ b/src/sdeventplus/utility/timer.hpp
@@ -18,6 +18,8 @@
* whether or not it has expired since creation or since the last
* clearExpired() or restart(). The concept of expiration is
* orthogonal to the callback mechanism and can be ignored.
+ *
+ * See example/heartbeat_timer.cpp for usage examples.
*/
template <ClockId Id>
class Timer