iceoryx_doc  1.0.1
Public Member Functions | List of all members
iox::concurrent::PeriodicTask< T > Class Template Reference

This class periodically executes a callable specified by the template parameter. This can be a struct with a operator()() overload, a cxx::function_ref<void()> or std::fuction<void()>. More...

#include <periodic_task.hpp>

Public Member Functions

template<typename... Args>
 PeriodicTask (const PeriodicTaskManualStart_t, const posix::ThreadName_t taskName, Args &&... args) noexcept
 Creates a periodic task. The specified callable is stored but not executed. To run the task, void start(const units::Duration interval) must be called. More...
 
template<typename... Args>
 PeriodicTask (const PeriodicTaskAutoStart_t, const units::Duration interval, const posix::ThreadName_t taskName, Args &&... args) noexcept
 Creates a periodic task by spawning a thread. The specified callable is executed immediately on creation and then periodically after the interval duration. More...
 
 ~PeriodicTask () noexcept
 Stops and joins the thread spawned by the constructor. More...
 
 PeriodicTask (const PeriodicTask &)=delete
 
 PeriodicTask (PeriodicTask &&)=delete
 
PeriodicTaskoperator= (const PeriodicTask &)=delete
 
PeriodicTaskoperator= (PeriodicTask &&)=delete
 
void start (const units::Duration interval) noexcept
 Spawns a thread and immediately executes the callable specified with the constructor. The execution is repeated after the specified interval is passed. More...
 
void stop () noexcept
 This stops the thread if it's running, otherwise does nothing. When this method returns, the thread is stopped. More...
 
bool isActive () const noexcept
 This method check if a thread is spawned and running, potentially executing a task. More...
 

Detailed Description

template<typename T>
class iox::concurrent::PeriodicTask< T >

This class periodically executes a callable specified by the template parameter. This can be a struct with a operator()() overload, a cxx::function_ref<void()> or std::fuction<void()>.

#include <iceoryx_utils/internal/concurrent/periodic_task.hpp>
#include <iceoryx_utils/internal/units/duration.hpp>
#include <iostream>
int main()
{
using namespace iox::units::duration_literals;
PeriodicTaskAutoStart, 1_s, "MyTask", [] { std::cout << "Hello World" << std::endl; }};
return 0;
}
PeriodicTask(const PeriodicTaskManualStart_t, const posix::ThreadName_t taskName, Args &&... args) noexcept
Creates a periodic task. The specified callable is stored but not executed. To run the task,...
Definition: periodic_task.inl:26
Definition: function_ref.hpp:32
Note
Currently execution time of the callable is added to the interval.
Template Parameters
Tis a callable type without function parameters

Constructor & Destructor Documentation

◆ PeriodicTask() [1/2]

template<typename T >
template<typename... Args>
iox::concurrent::PeriodicTask< T >::PeriodicTask ( const  PeriodicTaskManualStart_t,
const posix::ThreadName_t  taskName,
Args &&...  args 
)
inlinenoexcept

Creates a periodic task. The specified callable is stored but not executed. To run the task, void start(const units::Duration interval) must be called.

Template Parameters
Argsare variadic template parameter for which are forwarded to the underlying callable object
Parameters
[in]PeriodicTaskManualStart_tindicates that this ctor doesn't start the task; just pass PeriodicTaskManualStart as argument
[in]taskNamewill be set as thread name
[in]argsare forwarded to the underlying callable object

◆ PeriodicTask() [2/2]

template<typename T >
template<typename... Args>
iox::concurrent::PeriodicTask< T >::PeriodicTask ( const  PeriodicTaskAutoStart_t,
const units::Duration  interval,
const posix::ThreadName_t  taskName,
Args &&...  args 
)
inlinenoexcept

Creates a periodic task by spawning a thread. The specified callable is executed immediately on creation and then periodically after the interval duration.

Template Parameters
Argsare variadic template parameter for which are forwarded to the underlying callable object
Parameters
[in]PeriodicTaskAutoStart_tindicates that this ctor starts the task; just pass PeriodicTaskAutoStart as argument
[in]intervalis the time the thread waits between two invocations of the callable
[in]taskNamewill be set as thread name
[in]argsare forwarded to the underlying callable object

◆ ~PeriodicTask()

template<typename T >
iox::concurrent::PeriodicTask< T >::~PeriodicTask
inlinenoexcept

Stops and joins the thread spawned by the constructor.

Note
This is blocking and the blocking time depends on the callable.

Member Function Documentation

◆ isActive()

template<typename T >
bool iox::concurrent::PeriodicTask< T >::isActive
inlinenoexcept

This method check if a thread is spawned and running, potentially executing a task.

Returns
true if the thread is running, false otherwise.

◆ start()

template<typename T >
void iox::concurrent::PeriodicTask< T >::start ( const units::Duration  interval)
inlinenoexcept

Spawns a thread and immediately executes the callable specified with the constructor. The execution is repeated after the specified interval is passed.

Parameters
[in]intervalis the time the thread waits between two invocations of the callable
Attention
If the PeriodicTask instance has already a running thread, this will be stopped and started again with the new interval. This might take some time if a slow task is executing during this call.

◆ stop()

template<typename T >
void iox::concurrent::PeriodicTask< T >::stop
inlinenoexcept

This stops the thread if it's running, otherwise does nothing. When this method returns, the thread is stopped.

Attention
This might take some time if a slow task is executing during this call.

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