ALib C++ Framework
by
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
alib::threadmodel::Trigger Class Reference

Description:

The Trigger class provides a mechanism to periodically "trigger" actions on objects that implement the Triggered interface without requiring dedicated threads per object or manual additions of actions in existing threads.

The class manages a collection of Triggered objects and ensures that their virtual trigger() methods are called periodically based on the time interval given with their respective method triggerPeriod().

Internally, Trigger operates its own thread to handle the timing and execution of the triggers, adhering to the specified conditions.

This design helps in simplifying periodic task management within an application, avoiding thread proliferation and minimizing resource overhead.

Key responsibilities of the class:
  • Maintain and manage a list of Triggered objects.
  • Schedule and execute periodic triggers for the registered objects.
  • Provide the ability to add or remove Triggered objects dynamically.
Usage:
  • Users register their implementations of the Triggered interface with the Add method to begin periodic triggers.
  • Objects can be unregistered using the Remove() method.
  • The Stop method terminates the execution of the internal thread.

Intended for scenarios where lightweight, periodic task scheduling is needed without creating additional complexity or significant overhead.

Definition at line 97 of file trigger.inl.

Inheritance diagram for alib::threadmodel::Trigger:
alib::threads::Thread alib::threads::TCondition< T > alib::threads::Runnable

Inner Type Index:

struct  TriggerEntry
 The entry type used with field triggerList. More...

Public Method Index:

 Trigger ()
 Constructor.
virtual ~Trigger () override
 Destructor.
void Add (Triggered &triggered, bool initialWakeup=false)
void Do (Ticks until)
void Do (Ticks::Duration until)
void Remove (Triggered &triggered)
virtual void Run () override
 Implementation of the parent interface (virtual abstract).
virtual void Start ()
virtual void Stop ()
 Stops the trigger thread and joins it.

Protected Field Index:

bool internalThreadMode = false
 Denotes whether or not the trigger is currently used in internal thread mode.
ListMA< TriggerEntrytriggerList
 The list of registered triggered objects.
bool wakeUpCondition = false
Protected Field Index: inherited from alib::threads::Thread
std::thread * c11Thread =nullptr
 The internal C++ thread object.
ThreadID id =0
 The id of the thread.
const charactername =nullptr
 The name of the thread.
std::thread::id nativeID
 The internal C++ thread id.
Runnablerunnable =nullptr
 The runnable to execute.
State state = State::Unstarted
 Internal flag to indicate if the thread is alive.
Protected Field Index: inherited from alib::threads::TCondition< T >
std::condition_variable conditionVariable
 The condition variable used for blocking and notification.
DbgConditionAsserter Dbg
 The debug tool instance.
std::mutex mutex
 The mutex used for locking this instance.

Protected Method Index:

bool isConditionMet () const noexcept
Protected Method Index: inherited from alib::threads::Thread
void destruct ()
 Thread (const character *pName=A_CHAR(""))
 Thread (const Thread &)=delete
 Deleted copy constructor.
 Thread (Runnable *target, const character *pName=A_CHAR(""))
virtual ~Thread () override
ThreadID GetID () const
virtual const characterGetName () const
std::thread::id GetNativeID () const
State GetState ()
bool IsAlive ()
virtual void Join ()
virtual void SetName (const character *newName)
Protected Method Index: inherited from alib::threads::Runnable
virtual ~Runnable ()
 Virtual destructor.
Protected Method Index: inherited from alib::threads::TCondition< T >
 TCondition (const character *dbgName)
void Acquire (ALIB_DBG_TAKE_CI)
T & cast ()
void Release (ALIB_DBG_TAKE_CI)
void ReleaseAndNotify (ALIB_DBG_TAKE_CI)
void ReleaseAndNotifyAll (ALIB_DBG_TAKE_CI)
void WaitForNotification (ALIB_DBG_TAKE_CI)
void WaitForNotification (const Ticks &wakeUpTime, const CallerInfo &ci)
void WaitForNotification (const Ticks::Duration &maxWaitTimeSpan, const CallerInfo &ci)
void WaitForNotification (const Ticks::Duration::TDuration &maxWaitTimeSpan, const CallerInfo &ci)

Additional Inherited Members

Protected Type Index: inherited from alib::threads::Thread
enum class  State {
  Unstarted = 0 , Started = 1 , Running = 2 , Done = 3 ,
  Terminated = 4
}
Protected Static Method Index: inherited from alib::threads::Thread
static ThreadGet (std::thread::id nativeID)
static ThreadGetCurrent ()
static ThreadGetMain ()
static void Sleep (const Ticks::Duration &duration)
static void Sleep (const Ticks::Duration::TDuration &duration)
static void SleepMicros (int64_t microseconds)
static void SleepMillis (int milliseconds)
static void SleepNanos (int64_t nanoseconds)
static void SleepUntil (const Ticks &time)
static void YieldToSystem ()

◆ lang::Owner< Trigger & >

friend class lang::Owner< Trigger & >
friend

Definition at line 64 of file trigger.inl.

◆ threads::TCondition< Trigger >

friend struct threads::TCondition< Trigger >
friend

Definition at line 64 of file trigger.inl.

Field Details:

◆ internalThreadMode

bool alib::threadmodel::Trigger::internalThreadMode = false
protected

Denotes whether or not the trigger is currently used in internal thread mode.

Definition at line 128 of file trigger.inl.

◆ triggerList

ListMA<TriggerEntry> alib::threadmodel::Trigger::triggerList
protected

The list of registered triggered objects.

Definition at line 121 of file trigger.inl.

◆ wakeUpCondition

bool alib::threadmodel::Trigger::wakeUpCondition = false
protected

The condition requested by parent class TCondition via a call to isConditionMet.

Definition at line 125 of file trigger.inl.

Constructor(s) / Destructor Details:

◆ Trigger()

alib::threadmodel::Trigger::Trigger ( )

Constructor.

Definition at line 31 of file trigger.cpp.

◆ ~Trigger()

alib::threadmodel::Trigger::~Trigger ( )
overridevirtual

Destructor.

Definition at line 36 of file trigger.cpp.

Method Details:

◆ Add()

void alib::threadmodel::Trigger::Add ( Triggered & triggered,
bool initialWakeup = false )

Add an object to be triggered.

Parameters
triggeredThe object to be triggered.
initialWakeupIf true, the first wakeup is scheduled right away. Defaults to false.

Definition at line 45 of file trigger.cpp.

◆ Do() [1/2]

void alib::threadmodel::Trigger::Do ( Ticks until)

Executes the processing of triggers for up to a given maximum time. If the internal thread is not used, this method may be called manually inside an external loop to execute triggering operations within the specified timeframe.

If the internal thread was created and is running, with debug-compilations, an ALib error will be raised.

Parameters
untilDefines the future point in time until triggering is performed.

Definition at line 118 of file trigger.cpp.

◆ Do() [2/2]

void alib::threadmodel::Trigger::Do ( Ticks::Duration until)
inline

Invokes Do(Ticks) by adding the given duration to the current point in time.

Parameters
untilDefines the maximum duration for which this method will execute the trigger logic.

Definition at line 158 of file trigger.inl.

◆ isConditionMet()

bool alib::threadmodel::Trigger::isConditionMet ( ) const
inlineprotectednoexcept

Implementation of the interface needed by parent TCondition.

Returns
Member wakeUpCondition

Definition at line 132 of file trigger.inl.

◆ Remove()

void alib::threadmodel::Trigger::Remove ( Triggered & triggered)

Remove a previously added triggered object.

Parameters
triggeredThe object to be removed from the list.

Definition at line 75 of file trigger.cpp.

◆ Run()

void alib::threadmodel::Trigger::Run ( )
overridevirtual

Implementation of the parent interface (virtual abstract).

Reimplemented from alib::threads::Thread.

Definition at line 96 of file trigger.cpp.

◆ Start()

void alib::threads::Thread::Start ( )
virtual

Starts the execution of the thread. Method Run is invoked by the new system thread, which - if not overwritten - invokes the method virtual void Run() =0. Of course, this method immediately returns, and after invocation, method IsAlive will usually return true (unless the executed thread is not finished already).

Reimplemented from alib::threads::Thread.

Definition at line 271 of file thread.cpp.

◆ Stop()

void alib::threadmodel::Trigger::Stop ( )
virtual

Stops the trigger thread and joins it.

Definition at line 108 of file trigger.cpp.


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