omni::chrono::async_timer

SYNOPSIS

A timer class that invokes a basic tick event asynchronously.

top

DESCRIPTION

This timer will invoke a tick event at a specified interval regardless of how long the event delegate takes to complete. In other words, every time the timer fires, a new thread is spawned invoking the attached delegate. If the timer is stopped before the interval has elapsed, no subsequent tick events will fire, however, any currently running tick events will finish.

top

NOTES

Since the timer event happens on a new thread, care must be taken to make sure the attached delegate is re-entrant. Additionally, you must be aware that it is possible to have 2 events fire simultaneously if the attached delegate executes for longer than the specified interval.

top


EXAMPLE

#include <omnilib>

static void timer_func(omni::chrono::tick_t tick, const omni::generic_ptr& so)
{
    std::cout << "monotonic tick count = " << tick << std::endl;
}

int main(int argc, char* argv[])
{
    omni::chrono::async_timer timer(2000);
    timer.tick += timer_func;
    std::cout << "Starting the timer" << std::endl;
    timer.start();
    omni::sync::sleep(6000);
    std::cout << "Stopping the timer" << std::endl;
    timer.stop();
    return 0;
}
Visit the examples page for more.

top