18#ifndef __FPSDK_COMMON_THREAD_HPP__
19#define __FPSDK_COMMON_THREAD_HPP__
23#include <condition_variable>
97 std::condition_variable cond_;
203 bool Start(
const bool try_catch =
true);
277 std::unique_ptr<std::thread> thread_;
282 std::atomic<bool> abort_ =
false;
283 std::atomic<bool> started_ =
false;
287 void _Thread(
const bool try_catch);
A binary semaphore, useful for thread synchronisation.
WaitRes WaitUntil(const uint32_t period, const uint32_t min_sleep=0)
Wait (take), with timout aligned to a period.
WaitRes WaitFor(const uint32_t millis)
Wait (take), with timeout.
void Notify()
Notify ("signal", "give")
Helper class for handling threads.
Status GetStatus() const
Check thread status.
Thread(const std::string &name, ThreadFunc func, void *arg=nullptr, PrepFunc prep=nullptr, CleanFunc clean=nullptr)
Constructor.
WaitRes SleepUntil(const uint32_t period, const uint32_t min_sleep=0)
Sleep until next period start or woken up.
std::function< void(void *)> PrepFunc
Thread prepare function.
bool ShouldAbort()
Check if we should abort.
void Wakeup()
Wakup a sleeping thread.
std::function< void(void *)> CleanFunc
Thread cleanup function.
@ RUNNING
Running (Start(ed) and happily running)
@ FAILED
Failed (was Start()ed, but crashed due to an exception)
@ STOPPED
Stopped (not Start()ed, or properly and happily Stop()ped)
bool Stop()
Stop the thread.
~Thread()
Destructor, blocks until thread has stopped.
std::function< bool(Thread *, void *)> ThreadFunc
Thread main function.
WaitRes Sleep(const uint32_t millis)
Sleep until timeout or woken up.
const std::string & GetName()
Get thread name.
bool Start(const bool try_catch=true)
Start the thread.
WaitRes
Thread sleep result.
@ TIMEOUT
Sleep or wait timeout has expired (no wakeup, no interrupt)
@ WOKEN
Thread was woken up (sleep interrupted), or semaphore was taken (wait interrupted)
void SetThreadName(const std::string &name)
Set thread name.
std::size_t ThisThreadId()
Get numeric thread ID.