The Gaudi Framework  master (37c0b60a)
Gaudi::Utils::PeriodicAction Class Reference

Helper to periodically run asynchronous tasks. More...

#include </builds/gaudi/Gaudi/GaudiUtils/include/Gaudi/Utils/PeriodicAction.h>

Collaboration diagram for Gaudi::Utils::PeriodicAction:

Public Types

using clock = std::chrono::system_clock
 
using time_point = clock::time_point
 
using callback_t = std::function< void()>
 

Public Member Functions

 PeriodicAction (callback_t callback, std::chrono::milliseconds period_duration, bool autostart=true)
 
 PeriodicAction (PeriodicAction &&)=default
 
PeriodicActionoperator= (PeriodicAction &&)=default
 
 ~PeriodicAction ()
 
void start ()
 
void stop ()
 

Private Attributes

std::thread m_thread
 
std::promise< void > m_stop_thread
 
callback_t m_callback
 
std::chrono::milliseconds m_period_duration
 

Detailed Description

Helper to periodically run asynchronous tasks.

An instance of this starts a dedicated thread that periodically runs the user specified callback.

The thread starts as soon as the instance is created and it's automatically terminated (and joined) on destruction. It's also possible to defer the start (if requested at construction time) and anticipate the stop.

Definition at line 29 of file PeriodicAction.h.

Member Typedef Documentation

◆ callback_t

Definition at line 33 of file PeriodicAction.h.

◆ clock

◆ time_point

using Gaudi::Utils::PeriodicAction::time_point = clock::time_point

Definition at line 32 of file PeriodicAction.h.

Constructor & Destructor Documentation

◆ PeriodicAction() [1/2]

PeriodicAction::PeriodicAction ( callback_t  callback,
std::chrono::milliseconds  period_duration,
bool  autostart = true 
)

Definition at line 16 of file PeriodicAction.cpp.

17  : m_callback{ std::move( callback ) }, m_period_duration{ period_duration } {
18  if ( autostart ) start();
19 }

◆ PeriodicAction() [2/2]

Gaudi::Utils::PeriodicAction::PeriodicAction ( PeriodicAction &&  )
default

◆ ~PeriodicAction()

PeriodicAction::~PeriodicAction ( )

Definition at line 21 of file PeriodicAction.cpp.

21 { stop(); }

Member Function Documentation

◆ operator=()

PeriodicAction& Gaudi::Utils::PeriodicAction::operator= ( PeriodicAction &&  )
default

◆ start()

void PeriodicAction::start ( )

Definition at line 23 of file PeriodicAction.cpp.

23  {
24  if ( !m_thread.joinable() ) {
25  // Note: we can move the callback because we are not going to use it
26  // outside of the thread
28  stop_signal = m_stop_thread.get_future()] {
29  auto next_call = clock::now() + period_duration;
30  while ( stop_signal.wait_until( next_call ) == std::future_status::timeout ) {
31  callback();
32  // ensure the next call is at a multiple
33  // of m_period_duration after the last one
34  const auto now = clock::now();
35  while ( next_call < now ) next_call += period_duration;
36  }
37  } };
38  }
39 }

◆ stop()

void PeriodicAction::stop ( )

Definition at line 41 of file PeriodicAction.cpp.

41  {
42  if ( m_thread.joinable() ) {
44  m_thread.join();
45  }
46 }

Member Data Documentation

◆ m_callback

callback_t Gaudi::Utils::PeriodicAction::m_callback
private

Definition at line 47 of file PeriodicAction.h.

◆ m_period_duration

std::chrono::milliseconds Gaudi::Utils::PeriodicAction::m_period_duration
private

Definition at line 48 of file PeriodicAction.h.

◆ m_stop_thread

std::promise<void> Gaudi::Utils::PeriodicAction::m_stop_thread
private

Definition at line 46 of file PeriodicAction.h.

◆ m_thread

std::thread Gaudi::Utils::PeriodicAction::m_thread
private

Definition at line 45 of file PeriodicAction.h.


The documentation for this class was generated from the following files:
Gaudi::Utils::PeriodicAction::stop
void stop()
Definition: PeriodicAction.cpp:41
std::move
T move(T... args)
Gaudi::Utils::PeriodicAction::m_stop_thread
std::promise< void > m_stop_thread
Definition: PeriodicAction.h:46
std::promise::set_value
T set_value(T... args)
Gaudi::Utils::PeriodicAction::m_period_duration
std::chrono::milliseconds m_period_duration
Definition: PeriodicAction.h:48
std::promise::get_future
T get_future(T... args)
std::thread::joinable
T joinable(T... args)
std::thread
STL class.
Gaudi::Utils::PeriodicAction::m_callback
callback_t m_callback
Definition: PeriodicAction.h:47
Gaudi::Utils::PeriodicAction::m_thread
std::thread m_thread
Definition: PeriodicAction.h:45
Gaudi::Utils::PeriodicAction::start
void start()
Definition: PeriodicAction.cpp:23
std::thread::join
T join(T... args)
gaudirun.callback
callback
Definition: gaudirun.py:202
std::chrono::system_clock::now
T now(T... args)