Aikido
aikido::common::ExecutorThread Class Referencefinal

ExecutorThread is a wrapper of std::thread that calls a callback periodically. More...

#include <aikido/common/ExecutorThread.hpp>

Public Member Functions

template<typename Duration >
 ExecutorThread (std::function< void()> callback, const Duration &period)
 Constructs from callback and period. More...
 
 ~ExecutorThread ()
 Default destructor. The thread stops as ExecutorThread is destructed. More...
 
bool isRunning () const
 Returns true if the thread is running. More...
 
void stop ()
 Stops the thread. More...
 

Private Member Functions

void spin ()
 The loop function that will be executed by the thread. More...
 

Private Attributes

std::function< void()> mCallback
 Callback to be periodically executed by the thread. More...
 
std::chrono::milliseconds mPeriod
 The callback is called in this period. More...
 
std::atomic< bool > mIsRunning
 Flag whether the thread is running. More...
 
std::thread mThread
 Thread. More...
 

Detailed Description

ExecutorThread is a wrapper of std::thread that calls a callback periodically.

If you want to let ExecutorThread calls multiple callbacks then consider using ExecutorMultiplexer.

[]() { std::cout << "running...\n"; }, std::chrono::milliseconds(10));
// thread is running
// The destructor of ExecutorThread stops the thread.
See also
ExecutorMultiplexer

Constructor & Destructor Documentation

◆ ExecutorThread()

template<typename Duration >
aikido::common::ExecutorThread::ExecutorThread ( std::function< void()>  callback,
const Duration &  period 
)

Constructs from callback and period.

The thread begins execution immediately upon construction.

Parameters
[in]callbackCallback to be repeatedly executed by the thread.
[in]periodThe period of calling the callback.

◆ ~ExecutorThread()

aikido::common::ExecutorThread::~ExecutorThread ( )

Default destructor. The thread stops as ExecutorThread is destructed.

Member Function Documentation

◆ isRunning()

bool aikido::common::ExecutorThread::isRunning ( ) const

Returns true if the thread is running.

◆ spin()

void aikido::common::ExecutorThread::spin ( )
private

The loop function that will be executed by the thread.

◆ stop()

void aikido::common::ExecutorThread::stop ( )

Stops the thread.

It is safe to call this function even when the thread already stopped.

Member Data Documentation

◆ mCallback

std::function<void()> aikido::common::ExecutorThread::mCallback
private

Callback to be periodically executed by the thread.

◆ mIsRunning

std::atomic<bool> aikido::common::ExecutorThread::mIsRunning
private

Flag whether the thread is running.

◆ mPeriod

std::chrono::milliseconds aikido::common::ExecutorThread::mPeriod
private

The callback is called in this period.

◆ mThread

std::thread aikido::common::ExecutorThread::mThread
private

Thread.

aikido::common::ExecutorThread::ExecutorThread
ExecutorThread(std::function< void()> callback, const Duration &period)
Constructs from callback and period.
Definition: ExecutorThread-impl.hpp:8