Helper class to manage a long-running thread (duration of event loop)
More...
#include <EventLoopUtils.h>
|
| void | waitForCond (std::unique_lock< std::mutex > &lock) |
| | Helper to wait for the condition.
|
| void | run () |
| | Main function executed by the thread.
|
|
| std::condition_variable | m_cond |
| | Condition for which the thread waits most of its lifetime.
|
| std::mutex | m_mutex |
| | Mutex used to notify the condition.
|
| std::atomic< bool > | m_keepRunning {true} |
| | The thread's inner while-loop condition variable.
|
| std::atomic< bool > | m_started {false} |
| | Flag whether the main loop of the thread has started and will listen to further notifications.
|
| std::atomic< bool > | m_finished {false} |
| | Flag whether the main loop of the thread has finished.
|
| std::function< void()> | m_callback |
| | The callback executed in each step of the thread's inner while-loop.
|
| int | m_callbackIntervalMilliseconds {-1} |
| | If positive, call the callback periodically with this interval regardless of the m_cond.
|
| std::unique_ptr< std::thread > | m_thread |
| | The thread object.
|
Helper class to manage a long-running thread (duration of event loop)
Definition at line 16 of file EventLoopUtils.h.
◆ LoopThread() [1/3]
| HLT::LoopThread::LoopThread |
( |
std::function< void()> && | callback, |
|
|
int | callbackInterval = -1 ) |
|
inlineexplicit |
Definition at line 63 of file EventLoopUtils.h.
66 m_thread(std::make_unique<std::thread>([
this]{
run();})) {}
std::unique_ptr< std::thread > m_thread
The thread object.
void run()
Main function executed by the thread.
std::function< void()> m_callback
The callback executed in each step of the thread's inner while-loop.
int m_callbackIntervalMilliseconds
If positive, call the callback periodically with this interval regardless of the m_cond.
◆ ~LoopThread()
| HLT::LoopThread::~LoopThread |
( |
| ) |
|
|
inline |
Definition at line 68 of file EventLoopUtils.h.
68 {
70 }
void wait()
Wait until main loop finishes.
◆ LoopThread() [2/3]
| HLT::LoopThread::LoopThread |
( |
const LoopThread & | | ) |
|
|
delete |
◆ LoopThread() [3/3]
◆ cond()
| std::condition_variable & HLT::LoopThread::cond |
( |
| ) |
|
|
inline |
Definition at line 106 of file EventLoopUtils.h.
std::condition_variable m_cond
Condition for which the thread waits most of its lifetime.
◆ mutex()
| std::mutex & HLT::LoopThread::mutex |
( |
| ) |
|
|
inline |
Definition at line 107 of file EventLoopUtils.h.
std::mutex m_mutex
Mutex used to notify the condition.
◆ operator=() [1/2]
◆ operator=() [2/2]
◆ run()
| void HLT::LoopThread::run |
( |
| ) |
|
|
inlineprivate |
Main function executed by the thread.
Definition at line 45 of file EventLoopUtils.h.
45 {
47 std::unique_lock<std::mutex> lock{
m_mutex};
48
49
53
54
58 }
60 }
std::atomic< bool > m_started
Flag whether the main loop of the thread has started and will listen to further notifications.
void waitForCond(std::unique_lock< std::mutex > &lock)
Helper to wait for the condition.
std::atomic< bool > m_keepRunning
The thread's inner while-loop condition variable.
std::atomic< bool > m_finished
Flag whether the main loop of the thread has finished.
◆ start()
| void HLT::LoopThread::start |
( |
| ) |
|
|
inline |
Keep notifying the thread until the callback is called for the first time (returns just before calling the callback)
Definition at line 80 of file EventLoopUtils.h.
80 {
82 std::this_thread::sleep_for(std::chrono::milliseconds(3));
84 }
85 }
◆ stop()
| void HLT::LoopThread::stop |
( |
| ) |
|
|
inline |
◆ wait()
| void HLT::LoopThread::wait |
( |
| ) |
|
|
inline |
Wait until main loop finishes.
Definition at line 94 of file EventLoopUtils.h.
94 {
95
97
99 std::this_thread::sleep_for(std::chrono::milliseconds(3));
101 }
102
104 }
◆ waitForCond()
| void HLT::LoopThread::waitForCond |
( |
std::unique_lock< std::mutex > & | lock | ) |
|
|
inlineprivate |
Helper to wait for the condition.
Definition at line 36 of file EventLoopUtils.h.
36 {
39 } else {
41 }
42 }
◆ m_callback
| std::function<void()> HLT::LoopThread::m_callback |
|
private |
The callback executed in each step of the thread's inner while-loop.
Definition at line 29 of file EventLoopUtils.h.
◆ m_callbackIntervalMilliseconds
| int HLT::LoopThread::m_callbackIntervalMilliseconds {-1} |
|
private |
If positive, call the callback periodically with this interval regardless of the m_cond.
Definition at line 31 of file EventLoopUtils.h.
◆ m_cond
| std::condition_variable HLT::LoopThread::m_cond |
|
private |
◆ m_finished
| std::atomic<bool> HLT::LoopThread::m_finished {false} |
|
private |
Flag whether the main loop of the thread has finished.
Definition at line 27 of file EventLoopUtils.h.
◆ m_keepRunning
| std::atomic<bool> HLT::LoopThread::m_keepRunning {true} |
|
private |
The thread's inner while-loop condition variable.
Definition at line 23 of file EventLoopUtils.h.
◆ m_mutex
| std::mutex HLT::LoopThread::m_mutex |
|
private |
◆ m_started
| std::atomic<bool> HLT::LoopThread::m_started {false} |
|
private |
Flag whether the main loop of the thread has started and will listen to further notifications.
Definition at line 25 of file EventLoopUtils.h.
◆ m_thread
| std::unique_ptr<std::thread> HLT::LoopThread::m_thread |
|
private |
The documentation for this class was generated from the following file: