jaulib v1.3.0
Jau Support Library (C++, Java, ..)
Public Types | Public Member Functions | List of all members
jau::simple_timer Class Reference

A simple timer for timeout and interval applications, using one dedicated service_runner thread per instance. More...

#include <simple_timer.hpp>

Collaboration diagram for jau::simple_timer:

Public Types

typedef simple_timerTimer0_ref
 
typedef function< fraction_i64(Timer0_ref)> Timer_func
 User defined timer function using custom granularity via fraction_i64. More...
 

Public Member Functions

 simple_timer (const simple_timer &o)=delete
 No copy constructor nor move constructor. More...
 
 simple_timer (const std::string &name, const fraction_i64 &service_shutdown_timeout) noexcept
 Constructs a new service. More...
 
bool is_running () const noexcept
 Returns true if timer is running. More...
 
const std::string & name () const noexcept
 Return the given name of this timer. More...
 
bool shall_stop () const noexcept
 Returns true if timer shall stop. More...
 
bool start (const fraction_i64 &duration_, Timer_func tofunc) noexcept
 Start the timer with given user Timer_func function and initial duration. More...
 
void start_or_update (const fraction_i64 &duration_, Timer_func tofunc) noexcept
 Start or update the timer with given user Timer_func function and initial duration. More...
 
bool stop () noexcept
 Stop timer, see service_runner::stop() More...
 
pthread_t thread_id () const noexcept
 Return the thread-id of this timer's worker thread, zero if not running. More...
 

Detailed Description

A simple timer for timeout and interval applications, using one dedicated service_runner thread per instance.

Discussion: It is contemplated to add an implementation using a unique singleton service_runner for multiple timer instances via event loops.

Definition at line 52 of file simple_timer.hpp.

Member Typedef Documentation

◆ Timer0_ref

Definition at line 54 of file simple_timer.hpp.

◆ Timer_func

User defined timer function using custom granularity via fraction_i64.

Function gets invoked for each timer event, i.e. after reaching the duration set earlier.

Returns
duration in fractions of seconds for the next timer event or zero to end the timer thread.

Definition at line 64 of file simple_timer.hpp.

Constructor & Destructor Documentation

◆ simple_timer() [1/2]

simple_timer::simple_timer ( const std::string &  name,
const fraction_i64 service_shutdown_timeout 
)
noexcept

Constructs a new service.

Parameters
namethread name of this service
service_shutdown_timeoutmaximum duration in fractions of seconds to wait for service to stop at stop(), where fractions_i64::zero waits infinitely

Definition at line 61 of file simple_timer.cpp.

◆ simple_timer() [2/2]

jau::simple_timer::simple_timer ( const simple_timer o)
delete

No copy constructor nor move constructor.

Parameters
o

Member Function Documentation

◆ name()

const std::string & jau::simple_timer::name ( ) const
inlinenoexcept

Return the given name of this timer.

Definition at line 92 of file simple_timer.hpp.

◆ thread_id()

pthread_t jau::simple_timer::thread_id ( ) const
inlinenoexcept

Return the thread-id of this timer's worker thread, zero if not running.

Definition at line 97 of file simple_timer.hpp.

◆ is_running()

bool jau::simple_timer::is_running ( ) const
inlinenoexcept

Returns true if timer is running.

Definition at line 102 of file simple_timer.hpp.

Here is the caller graph for this function:

◆ shall_stop()

bool jau::simple_timer::shall_stop ( ) const
inlinenoexcept

Returns true if timer shall stop.

This flag can be used by the Timer_func function to determine whether to skip lengthly tasks.

Definition at line 109 of file simple_timer.hpp.

Here is the caller graph for this function:

◆ start()

bool simple_timer::start ( const fraction_i64 duration_,
Timer_func  tofunc 
)
noexcept

Start the timer with given user Timer_func function and initial duration.

Parameters
duration_initial timer duration in fractions of seconds until next timer event
tofuncuser Timer_func to be called on next timer event
Returns
true if timer has been started, otherwise false implies timer is already running.

Definition at line 66 of file simple_timer.cpp.

Here is the caller graph for this function:

◆ start_or_update()

void simple_timer::start_or_update ( const fraction_i64 duration_,
Timer_func  tofunc 
)
noexcept

Start or update the timer with given user Timer_func function and initial duration.

This is faster than calling stop() and start(), however, an already started timer user Timer_func will proceed.

Parameters
duration_initial timer duration in fractions of seconds until next timer event
tofuncuser Timer_func to be called on next timer event

Definition at line 76 of file simple_timer.cpp.

◆ stop()

bool jau::simple_timer::stop ( )
inlinenoexcept

Stop timer, see service_runner::stop()

Returns
true if timer has been stopped or false if timeout has been hit

Definition at line 136 of file simple_timer.hpp.

Here is the caller graph for this function:

The documentation for this class was generated from the following files: