A simple demonstration of using timers.
A simple demonstration of using timers.This example creates and event loop and starts two timers to print fizz and buzz every 3 respective 5 seconds.
The user can also configure a third timer via stdin to print woof.
#include <iostream>
#include <string>
struct interval_change_type;
{
public:
:
fz::event_handler(loop)
{
fizz_ =
add_timer(fz::duration::from_seconds(3),
false);
buzz_ =
add_timer(fz::duration::from_seconds(5),
false);
}
{
}
private:
{
}
{
}
void on_timer(fz::timer_id const& id)
{
if (id == fizz_) {
std::cout << "fizz" << std::endl;
}
else if (id == buzz_) {
std::cout << "buzz" << std::endl;
}
else if (id == woof_) {
std::cout << "woof" << std::endl;
}
}
fz::timer_id fizz_{};
fz::timer_id buzz_{};
fz::timer_id woof_{};
};
int main()
{
fizzbuzz fb(loop);
std::cout << "Enter interval in seconds for the woof timer or 0 to stop program." << std::endl;
int seconds;
do {
std::cin >> seconds;
if (std::cin.good() && seconds > 0) {
fb.send_event<interval_change>(fz::duration::from_seconds(seconds));
}
}
while (std::cin.good() && seconds != 0);
return 0;
}
The duration class represents a time interval in milliseconds.
Definition time.hpp:291
Common base class for all events.
Definition event.hpp:23
Simple handler for asynchronous event processing.
Definition event_handler.hpp:55
void stop_timer(timer_id id)
void remove_handler()
Deactivates handler, removes all pending events and stops all timers for this handler.
virtual void operator()(event_base const &)=0
Called by the event loop in the worker thread with the event to process.
timer_id add_timer(monotonic_clock const &deadline, duration const &interval={})
Adds a timer, returns the timer id.
A threaded event loop that supports sending events and timers.
Definition event_loop.hpp:34
This is the recommended event class.
Definition event.hpp:68
Declares the event_handler class.
The namespace used by libfilezilla.
Definition apply.hpp:17
bool dispatch(event_base const &ev, F &&f)
Dispatch for simple_event<> based events to simple functors.
Definition event_handler.hpp:199
Assorted classes dealing with time.