4#ifndef TRIGSERVICES_EVENTLOOPUTILS_H
5#define TRIGSERVICES_EVENTLOOPUTILS_H
7#include <condition_variable>
46 std::unique_lock<std::mutex> lock{
m_mutex};
62 explicit LoopThread(std::function<
void()>&& callback,
int callbackInterval=-1)
81 std::this_thread::sleep_for(std::chrono::milliseconds(3));
98 std::this_thread::sleep_for(std::chrono::milliseconds(3));
std::unique_ptr< std::thread > m_thread
The thread object.
void run()
Main function executed by the thread.
void waitForCond(std::unique_lock< std::mutex > &lock)
Helper to wait for the condition.
void wait()
Wait until main loop finishes.
std::condition_variable m_cond
Condition for which the thread waits most of its lifetime.
void start()
Keep notifying the thread until the callback is called for the first time (returns just before callin...
bool m_finished
Flag whether the main loop of the thread has finished.
LoopThread(LoopThread &&)=delete
LoopThread & operator=(const LoopThread &)=delete
std::mutex m_mutex
Mutex used to notify the condition.
bool m_started
Flag whether the main loop of the thread has started and will listen to further notifications.
bool m_keepRunning
The thread's inner while-loop condition variable.
LoopThread(const LoopThread &)=delete
LoopThread & operator=(LoopThread &&)=delete
std::function< void()> m_callback
The callback executed in each step of the thread's inner while-loop.
void stop()
Flag the main loop to finish.
int m_callbackIntervalMilliseconds
If positive, call the callback periodically with this interval regardless of the m_cond.
std::condition_variable & cond()
LoopThread(std::function< void()> &&callback, int callbackInterval=-1)
It used to be useful piece of code for replacing actual SG with other store of similar functionality ...