Skip to content

iox::concurrent::PeriodicTask🔗

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 Functions🔗

Name
template \<typename... Args>
PeriodicTask(const PeriodicTaskManualStart_t, const posix::ThreadName_t taskName, Args &&... args)
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 \<typename... Args>
PeriodicTask(const PeriodicTaskAutoStart_t, const units::Duration interval, const posix::ThreadName_t taskName, Args &&... args)
Creates a periodic task by spawning a thread. The specified callable is executed immediately on creation and then periodically after the interval duration.
~PeriodicTask()
Stops and joins the thread spawned by the constructor.
PeriodicTask(const PeriodicTask & )
PeriodicTask(PeriodicTask && )
PeriodicTask & operator=(const PeriodicTask & )
PeriodicTask & operator=(PeriodicTask && )
void start(const units::Duration interval)
Spawns a thread and immediately executes the callable specified with the constructor. The execution is repeated after the specified interval is passed.
void stop()
This stops the thread if it's running, otherwise does nothing. When this method returns, the thread is stopped.
bool isActive() const
This method check if a thread is spawned and running, potentially executing a task.

Detailed Description🔗

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

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()>.

Template Parameters:

  • T is a callable type without function parameters

Note: Currently execution time of the callable is added to the interval.

#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;
    PeriodicTask<iox::cxx::function_ref<void()>> task{
        PeriodicTaskAutoStart, 1_s, "MyTask", [] { std::cout << "Hello World" << std::endl; }};

        return 0;
}

Public Functions Documentation🔗

function PeriodicTask🔗

template <typename... Args>
inline PeriodicTask(
    const PeriodicTaskManualStart_t,
    const posix::ThreadName_t taskName,
    Args &&... args
)

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.

Parameters:

  • PeriodicTaskManualStart_t indicates that this ctor doesn't start the task; just pass PeriodicTaskManualStart as argument
  • taskName will be set as thread name
  • args are forwarded to the underlying callable object

Template Parameters:

  • Args are variadic template parameter for which are forwarded to the underlying callable object

function PeriodicTask🔗

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

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

Parameters:

  • PeriodicTaskAutoStart_t indicates that this ctor starts the task; just pass PeriodicTaskAutoStart as argument
  • interval is the time the thread waits between two invocations of the callable
  • taskName will be set as thread name
  • args are forwarded to the underlying callable object

Template Parameters:

  • Args are variadic template parameter for which are forwarded to the underlying callable object

function ~PeriodicTask🔗

inline ~PeriodicTask()

Stops and joins the thread spawned by the constructor.

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

function PeriodicTask🔗

PeriodicTask(
    const PeriodicTask & 
)

function PeriodicTask🔗

PeriodicTask(
    PeriodicTask && 
)

function operator=🔗

PeriodicTask & operator=(
    const PeriodicTask & 
)

function operator=🔗

PeriodicTask & operator=(
    PeriodicTask && 
)

function start🔗

inline void start(
    const units::Duration interval
)

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

Parameters:

  • interval is 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.

function stop🔗

inline void stop()

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.

function isActive🔗

inline bool isActive() const

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

Return: true if the thread is running, false otherwise.


Updated on 31 May 2022 at 15:29:15 CEST