Aikido
ExecutorThread.hpp
Go to the documentation of this file.
1 #ifndef AIKIDO_COMMON_EXECUTORTHREAD_HPP_
2 #define AIKIDO_COMMON_EXECUTORTHREAD_HPP_
3 
4 #include <atomic>
5 #include <chrono>
6 #include <functional>
7 #include <thread>
8 
9 namespace aikido {
10 namespace common {
11 
28 class ExecutorThread final
29 {
30 public:
35  template <typename Duration>
36  ExecutorThread(std::function<void()> callback, const Duration& period);
37 
40 
42  bool isRunning() const;
43 
46  void stop();
47 
48 private:
50  void spin();
51 
52 private:
54  std::function<void()> mCallback;
55 
57  std::chrono::milliseconds mPeriod;
58 
60  std::atomic<bool> mIsRunning;
61 
63  std::thread mThread;
64 };
65 
66 } // namespace common
67 } // namespace aikido
68 
70 
71 #endif // AIKIDO_COMMON_EXECUTORTHREAD_HPP_
aikido::common::ExecutorThread::stop
void stop()
Stops the thread.
aikido
Format of serialized trajectory in YAML.
Definition: algorithm.hpp:4
aikido::common::ExecutorThread::spin
void spin()
The loop function that will be executed by the thread.
aikido::common::ExecutorThread::mPeriod
std::chrono::milliseconds mPeriod
The callback is called in this period.
Definition: ExecutorThread.hpp:57
aikido::common::ExecutorThread::isRunning
bool isRunning() const
Returns true if the thread is running.
aikido::common::ExecutorThread::ExecutorThread
ExecutorThread(std::function< void()> callback, const Duration &period)
Constructs from callback and period.
Definition: ExecutorThread-impl.hpp:8
aikido::common::ExecutorThread::mCallback
std::function< void()> mCallback
Callback to be periodically executed by the thread.
Definition: ExecutorThread.hpp:54
aikido::common::ExecutorThread::~ExecutorThread
~ExecutorThread()
Default destructor. The thread stops as ExecutorThread is destructed.
aikido::common::ExecutorThread::mIsRunning
std::atomic< bool > mIsRunning
Flag whether the thread is running.
Definition: ExecutorThread.hpp:60
aikido::common::ExecutorThread
ExecutorThread is a wrapper of std::thread that calls a callback periodically.
Definition: ExecutorThread.hpp:28
ExecutorThread-impl.hpp
aikido::common::ExecutorThread::mThread
std::thread mThread
Thread.
Definition: ExecutorThread.hpp:63