Template Class PeriodicTask

Class Documentation

template<typename T>
class 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()>.

#include <iceoryx_hoofs/internal/concurrent/periodic_task.hpp>
#include <iceoryx_hoofs/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;
}

Note

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

Template Parameters:

T – is a callable type without function parameters

Public 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.

Template Parameters:

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

Parameters:
  • PeriodicTaskManualStart_t – indicates that this ctor doesn’t start the task; just pass PeriodicTaskManualStart as argument

  • taskName[in] will be set as thread name

  • args[in] are forwarded to the underlying callable object

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.

Template Parameters:

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

Parameters:
  • PeriodicTaskAutoStart_t – indicates that this ctor starts the task; just pass PeriodicTaskAutoStart as argument

  • interval[in] is the time the thread waits between two invocations of the callable

  • taskName[in] will be set as thread name

  • args[in] are forwarded to the underlying callable object

~PeriodicTask() noexcept

Stops and joins the thread spawned by the constructor.

Note

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

PeriodicTask(const PeriodicTask&) = delete
PeriodicTask(PeriodicTask&&) = delete
PeriodicTask &operator=(const PeriodicTask&) = delete
PeriodicTask &operator=(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.

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.

Parameters:

interval[in] is the time the thread waits between two invocations of the callable

void stop() noexcept

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.

bool isActive() const noexcept

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

Returns:

true if the thread is running, false otherwise.