summaryrefslogtreecommitdiffstats
path: root/example/heartbeat_timer.cpp
blob: c8354e8aa487bf92dff40e6f66333fb98da288f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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();
}
OpenPOWER on IntegriCloud