10#include <condition_variable>
24 std::condition_variable m_condition;
25 unsigned long m_count = 0;
30 std::unique_lock<
decltype(m_mutex)> lock(m_mutex);
36 std::unique_lock<
decltype(m_mutex)> lock(m_mutex);
38 m_condition.notify_one();
43 std::unique_lock<
decltype(m_mutex)> lock(m_mutex);
45 m_condition.wait(lock);
50 bool wait_for(std::chrono::milliseconds timeout_ms) {
51 std::cv_status l_Stat = std::cv_status::no_timeout;
52 std::unique_lock<
decltype(m_mutex)> lock(m_mutex);
53 while (!m_count && l_Stat == std::cv_status::no_timeout)
54 l_Stat = m_condition.wait_for(lock, timeout_ms);
55 if (l_Stat == std::cv_status::no_timeout)
57 return (l_Stat == std::cv_status::no_timeout);
62 std::unique_lock<
decltype(m_mutex)> lock(m_mutex);
71 template <
typename MsgT>
75 std::queue<MsgT> m_MsgQ;
83 bool l_em = m_MsgQ.empty();
90 std::queue<MsgT> l_Empty;
91 std::swap(m_MsgQ, l_Empty);
114 bool l_Success =
false;
116 if (m_Sem.try_wait())
130 std::thread m_Thread;
132 std::chrono::milliseconds m_Period_ms;
135 std::function<void(
void)> m_NotifyEvent;
142 CTimer(
const std::function<
void(
void)> & rTimerEventNotifierFunction)
143 : m_NotifyEvent(rTimerEventNotifierFunction)
146 template<
class Fn,
class... Args>
147 CTimer(Fn&& rTimerEventNotifierFunction, Args&&... Ax)
148 : m_NotifyEvent(std::bind(rTimerEventNotifierFunction, Ax...))
160 bool l_Aborted = m_AbortSem.
wait_for(m_Period_ms);
180 std::lock_guard<
decltype(m_Lock)> l_lg(m_Lock);
182 if (m_Thread.joinable())
186 m_Period_ms = Delay_ms;
188 m_Thread = std::thread(&CTimer::ThreadMain,
this);
191 bool Start(std::chrono::milliseconds Period_ms)
193 std::lock_guard<
decltype(m_Lock)> l_lg(m_Lock);
195 if (m_Thread.joinable())
198 m_SingleShot =
false;
199 m_Period_ms = Period_ms;
201 m_Thread = std::thread(&CTimer::ThreadMain,
this);
206 std::lock_guard<
decltype(m_Lock)> l_lg(m_Lock);
208 if (m_Thread.joinable())
222 template <
typename UserMsgT>
236 sMailBoxMsg(
eEventType EvtType,
const UserMsgT & rMsg)
237 : Evt(EvtType), UserMsg(rMsg)
248 CMailBox<sMailBoxMsg> m_MailBox;
249 std::thread m_Thread;
250 std::atomic<bool> m_RequestedToStop;
259 : m_RequestedToStop(false)
280 template <
typename UserMsgT>
283 if (!m_Thread.joinable())
286 m_RequestedToStop =
false;
287 m_Thread = std::thread(&CThreadedMailBox::l_ThreadMain,
this);
291 template <
typename UserMsgT>
294 m_RequestedToStop =
true;
296 if (m_Thread.joinable() && std::this_thread::get_id() != m_Thread.get_id())
300 template <
typename UserMsgT>
306 template <
typename UserMsgT>
309 return m_RequestedToStop;
312 template <
typename UserMsgT>
313 void CThreadedMailBox<UserMsgT>::l_ThreadMain()
316 bool l_RunWorkerThread =
true;
317 while (l_RunWorkerThread)
322 case eEventType::kExit:
323 l_RunWorkerThread =
false;
324 l_ThreadTask(Msg.Evt,
nullptr);
326 case eEventType::kUserMsg:
327 l_ThreadTask(Msg.Evt, &Msg.UserMsg);
void reset()
Reset semaphore object.
bool wait_for(std::chrono::milliseconds timeout_ms)
Wait for a semaphore for a limited time.
void notify()
Raise a semaphore.
bool try_wait()
Check whether a semaphore has been raised.
void wait()
Wait for a semaphore.
CTimer(const std::function< void(void)> &rTimerEventNotifierFunction)
bool Start(std::chrono::milliseconds Period_ms)
bool StartSingleShot(std::chrono::milliseconds Delay_ms)
CTimer(Fn &&rTimerEventNotifierFunction, Args &&... Ax)
bool l_IsRequestedToStop()
virtual void l_ThreadTask(eEventType EvtType, UserMsgT *pMsg)=0
void l_SendMsg(const UserMsgT &rMsg)