Fixposition SDK 0.0.0-heads/main-0-g7b59b93
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<void(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
bool Start ()
 Start the thread.
 
void Stop ()
 Stop the thread.
 
void Wakeup ()
 Wakup a sleeping thread.
 
bool IsRunning ()
 Check if thread is running.
 

Worker thread methods

bool Sleep (const uint32_t millis)
 Sleep until timeout or woken up.
 
bool 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_.IsRunning()) {
sleep(500);
thread_.Wakeup() // = BinarySemaphore::Notify()
sleep(2000);
thread_.Wakeup() // = BinarySemaphore::Notify()
n++;
if (n >= 10) {
thread_.Stop();
}
}
}
private:
void Worker(void *) {
while (!thread_.ShouldAbort()) {
if (thread_.Sleep(1000)) { // = BinarySemaphore::SleepFor(1000)
// We have been woken up
} else {
// Timeout expired
}
}
}
Thread thread_;
};

Definition at line 144 of file thread.hpp.

Member Typedef Documentation

◆ ThreadFunc

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

Thread main function.

Definition at line 147 of file thread.hpp.

◆ PrepFunc

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

Thread prepare function.

Definition at line 148 of file thread.hpp.

◆ CleanFunc

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

Thread cleanup function.

Definition at line 149 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 ( )

Start the thread.

Note
This method is used by the main, controlling thread

◆ Stop()

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

Stop the thread.

Note
This method is used by the main, controlling thread

◆ Wakeup()

void fpsdk::common::thread::Thread::Wakeup ( )

Wakup a sleeping thread.

Note
This method is used by the main, controlling thread

◆ IsRunning()

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

Check if thread is running.

Returns
true if thread is running, false if it has stopped
Note
This method is used by the main, controlling thread

◆ Sleep()

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

Sleep until timeout or woken up.

Parameters
[in]millisNumber of [ms] to sleep
Returns
true if the thread has been woken up, false if the timeout has expired
Note
This method is used by the worker thread

◆ SleepUntil()

bool 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
true if the thread has been woken up, false if the timeout has expired or period was 0
Note
This method is used by the worker thread

◆ ShouldAbort()

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

Check if we should abort.

Note
This method is used by the worker thread, typically as the predicate in the main loop, e.g. while (!thread_.ShouldAbort()) { do_stuff(); }

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