Fixposition SDK 0.0.0-heads/main-0-gd0a6ce2
Collection of c++ libraries and apps for use with Fixposition products
Loading...
Searching...
No Matches
fpsdk::common::thread::Thread Class Reference

Helper class for handling threads. More...

#include <thread.hpp>

Public Types

using ThreadFunc = std::function<bool(Thread*, void*)>
 Thread main function.
 
using PrepFunc = std::function<void(void*)>
 Thread prepare function.
 
using CleanFunc = std::function<void(void*)>
 Thread cleanup function.
 

Public Member Functions

 Thread (const std::string &name, ThreadFunc func, void *arg=nullptr, PrepFunc prep=nullptr, CleanFunc clean=nullptr)
 Constructor.
 
 ~Thread ()
 Destructor, blocks until thread has stopped.
 
const std::string & GetName ()
 Get thread name.
 

Main (controlling) thread methods

enum  Status { STOPPED , RUNNING , FAILED }
 Thread status. More...
 
bool Start (const bool try_catch=true)
 Start the thread.
 
bool Stop ()
 Stop the thread.
 
void Wakeup ()
 Wakup a sleeping thread.
 
Status GetStatus () const
 Check thread status.
 

Worker thread methods

WaitRes Sleep (const uint32_t millis)
 Sleep until timeout or woken up.
 
WaitRes SleepUntil (const uint32_t period, const uint32_t min_sleep=0)
 Sleep until next period start or woken up.
 
bool ShouldAbort ()
 Check if we should abort.
 

Detailed Description

Helper class for handling threads.

Example:

class Example {
public:
Example() :
thread_ { "worker", std::bind(&Example::Worker, this, std::placeholders::_1) }
{ }
void Run() {
if (!thread_.Start()) {
throw "...";
}
int n = 0;
while (thread_.GetStatus() == Thread::Status::RUNNING) {
sleep(500);
thread_.Wakeup() // = BinarySemaphore::Notify()
sleep(2000);
thread_.Wakeup() // = BinarySemaphore::Notify()
n++;
if (n >= 10) {
thread_.Stop();
}
}
}
private:
bool Worker(void *) {
while (!thread_.ShouldAbort()) {
if (thread_.Sleep(123) == WaitRes::WOKEN) { // = BinarySemaphore::SleepFor(1000)
// We have been woken up
} else {
// Timeout expired
}
}
return true;
}
Thread thread_;
};

Definition at line 154 of file thread.hpp.

Member Typedef Documentation

◆ ThreadFunc

using fpsdk::common::thread::Thread::ThreadFunc = std::function<bool(Thread*, void*)>

Thread main function.

Definition at line 157 of file thread.hpp.

◆ PrepFunc

using fpsdk::common::thread::Thread::PrepFunc = std::function<void(void*)>

Thread prepare function.

Definition at line 158 of file thread.hpp.

◆ CleanFunc

using fpsdk::common::thread::Thread::CleanFunc = std::function<void(void*)>

Thread cleanup function.

Definition at line 159 of file thread.hpp.

Member Enumeration Documentation

◆ Status

Thread status.

Enumerator
STOPPED 

Stopped (not Start()ed, or properly and happily Stop()ped)

RUNNING 

Running (Start(ed) and happily running)

FAILED 

Failed (was Start()ed, but crashed due to an exception)

Definition at line 220 of file thread.hpp.

Constructor & Destructor Documentation

◆ Thread()

fpsdk::common::thread::Thread::Thread ( const std::string & name,
ThreadFunc func,
void * arg = nullptr,
PrepFunc prep = nullptr,
CleanFunc clean = nullptr )

Constructor.

Parameters
[in]nameName of the thread, for debugging
[in]funcThe thread function
[in]argOptional thread function user argument
[in]prepOptional prepare function, called before the thread function
[in]cleanOptional cleanup function, called after the threadd stopped (or crashed)
Note
Constructing an instance delibarately does not automatically start the thread!

Member Function Documentation

◆ GetName()

const std::string & fpsdk::common::thread::Thread::GetName ( )

Get thread name.

Returns
the thread name

◆ Start()

bool fpsdk::common::thread::Thread::Start ( const bool try_catch = true)

Start the thread.

Parameters
[in]try_catchRun user-supplied thread function in a try ... catch block
Returns
true if the thread was started, false otherwise

◆ Stop()

bool fpsdk::common::thread::Thread::Stop ( )

Stop the thread.

Returns
true if the thread was stopped, false otherwise

◆ GetStatus()

Status fpsdk::common::thread::Thread::GetStatus ( ) const

Check thread status.

Returns
the thread status

◆ Sleep()

WaitRes fpsdk::common::thread::Thread::Sleep ( const uint32_t millis)

Sleep until timeout or woken up.

Parameters
[in]millisNumber of [ms] to sleep
Returns
WaitRes::WOKEN if the thread has been woken up, WaitRes::TIMEOUT if the timeout has expired

◆ SleepUntil()

WaitRes fpsdk::common::thread::Thread::SleepUntil ( const uint32_t period,
const uint32_t min_sleep = 0 )

Sleep until next period start or woken up.

See BinarySemaphore::WaitUntil() for a detailed explanation.

Parameters
[in]periodPeriod duration [ms], must be > 0
[in]min_sleepMinimal sleep duration [ms], must be < period
Returns
WaitRes::WOKEN if the thread has been woken up, WaitRes::TIMEOUT if the timeout has expired or period was 0

The documentation for this class was generated from the following file: