Ocean
|
This class implements a thread. More...
Data Structures | |
class | ThreadId |
This class implements a platform independent wrapper for thread ids. More... | |
Public Types | |
enum | ThreadPriority { PRIORITY_IDLE , PRIORTY_BELOW_NORMAL , PRIORTY_NORMAL , PRIORTY_ABOVE_NORMAL , PRIORTY_HIGH , PRIORTY_REALTIME } |
Definition of different thread priority values. More... | |
Public Member Functions | |
Thread (const std::string &name=std::string()) | |
Creates a new thread object. More... | |
Thread (const unsigned int randomNumberSeedValue, const std::string &name=std::string()) | |
Creates a new thread object. More... | |
virtual | ~Thread () |
Destructs a thread object. More... | |
bool | startThread () |
Starts the thread. More... | |
void | stopThread () |
Informs the thread to stop. More... | |
bool | terminateThread () |
Terminates the thread. More... | |
bool | joinThread (const unsigned int timeout=(unsigned int)(-1)) |
Waits until this thread has been stopped. More... | |
bool | shouldThreadStop () const |
Returns whether this thread should stop. More... | |
bool | isThreadInvokedToStart () const |
Returns whether this thread has been invoked to start immediately. More... | |
bool | isThreadActive () const |
Returns whether this thread is active. More... | |
Static Public Member Functions | |
static void | sleep (unsigned int ms) |
Sleeps the calling thread for a given time. More... | |
static void | giveUp () |
Gives up the remaining thread time. More... | |
static ThreadId | currentThreadId () |
Returns the thread id of the current (calling) thread. More... | |
static ThreadPriority | threadPriority () |
Returns the priority of the current thread. More... | |
static bool | setThreadPriority (const ThreadPriority priority) |
Sets the priority of the current thread. More... | |
template<typename TObject , typename TExpectedValue > | |
static bool | waitForValue (TObject &object, const TExpectedValue &expectedValue, const double timeout=-1.0) |
Waits until an object/variable has an expected value. More... | |
template<typename TObject , typename TExpectedValue > | |
static bool | waitForValue (TObject &object, const TExpectedValue &expectedValue, TemporaryScopedLock &temporaryScopedLock, const double timeout=-1.0) |
Waits until an object/variable has an expected value. More... | |
static int | pthread_timedjoin_np (pthread_t thread, void **retval, const struct timespec *abstime) |
Implements a thread join function with timeout value. More... | |
Protected Types | |
typedef std::pair< pthread_t, bool > | TimedJoinPair |
Definition of a pair holding a thread id and a boolean state. More... | |
Protected Member Functions | |
Thread (const Thread &thread)=delete | |
Disabled copy constructor. More... | |
void | createThread () |
Creates the thread itself. More... | |
void | destroyThread () |
Destroys the thread itself. More... | |
void | stopThreadExplicitly (const unsigned int timeout=5000u) |
Tries to stop the thread gracefully. More... | |
virtual void | threadRun ()=0 |
This function has to be overloaded in derived class. More... | |
Thread & | operator= (const Thread &thread)=delete |
The disabled assign operator. More... | |
Private Member Functions | |
void | internalThreadRun () |
Platform independent internal thread function calling the external thread function. More... | |
Static Private Member Functions | |
static void * | pthread_timedjoin_np_helper (void *threadData) |
The helper function for the pthread_timedjoin_np() implementation. More... | |
static DWORD __stdcall | staticThreadRun (void *data) |
Internal thread function calling the external thread function. More... | |
static void * | staticThreadRun (void *data) |
Internal thread function calling the external thread function. More... | |
Private Attributes | |
HANDLE | threadHandle_ = nullptr |
Internal windows thread handle. More... | |
pthread_t | threadObject_ = 0 |
Internal pthread object. More... | |
std::atomic< bool > | threadShouldStop_ {false} |
Determines whether this thread should stop. More... | |
bool | threadIsActive_ = false |
Determines whether this thread is actually running. More... | |
bool | threadIsInvokedToStart_ = false |
Determines whether this thread has been invoked to start immediately. More... | |
std::string | threadName_ |
Name of the thread. More... | |
unsigned int | threadRandomNumberSeedValue_ = 0u |
The seed value for random number generators. More... | |
This class implements a thread.
The implementation can be used in two ways:
First: Derive an own class from this thread class and overwrite the internal Thread::threadRun() function.
This function will then be called once the thread has been started. If this run function returns the thread will be closed.
Second: Set the run callback function which will be called instead of the normal internal run function.
Similar to the first solution the thread will be closed after the callback function returns.
However, if the normal internal run function is overloaded by a derived call a possible defined callback function has no effect.
Each thread can be started using the Thread::startThread() function,
and stopped using the Thread::stopThread() function.
However, the stop function will not explicit terminate the thread, it sets the thread's should-stop-state only.
Therefore, an implementation using this thread class must check the thread's should-stop-state recurrently.
Use Thread::shouldThreadStop() to determine whether your implementation should stop the thread execution.
If the thread execution does not return after a Thread::stopThread() the thread can be kill in an explicit manner.
Beware: Such kind of rough termination should be avoided and in most cases evidences a dirty usage of the internal run function.
See this tutorial:
|
protected |
Definition of a pair holding a thread id and a boolean state.
Definition of different thread priority values.
|
explicit |
Creates a new thread object.
The thread will be initialized with a seed value automatically generated by using RandomI::random32().
name | Optional thread name which can be helpful for debugging tasks |
|
explicit |
Creates a new thread object.
randomNumberSeedValue | An explicit seed value for the random number initialization, with range [0, infinity) |
name | Optional thread name which can be helpful for debugging tasks |
|
virtual |
Destructs a thread object.
|
protecteddelete |
Disabled copy constructor.
thread | The object that would be copied |
|
protected |
Creates the thread itself.
|
static |
Returns the thread id of the current (calling) thread.
|
protected |
Destroys the thread itself.
However the thread must be terminated before!
|
static |
Gives up the remaining thread time.
|
private |
Platform independent internal thread function calling the external thread function.
bool Ocean::Thread::isThreadActive | ( | ) | const |
Returns whether this thread is active.
An active thread currently executes the internal thread function.
bool Ocean::Thread::isThreadInvokedToStart | ( | ) | const |
Returns whether this thread has been invoked to start immediately.
Beware: No information is provided whether the thread is active already. However, to not start a thread invoked to start again, instead wait for the termination.
bool Ocean::Thread::joinThread | ( | const unsigned int | timeout = (unsigned int)(-1) | ) |
Waits until this thread has been stopped.
timeout | The number of milliseconds the caller thread will wait for this thread, -1 to wait infinite |
The disabled assign operator.
thread | The object that would be assigned |
|
static |
Implements a thread join function with timeout value.
Depending on the platform, this function may not exist in the default libraries (e.g., on Apple platforms).
thread | The thread for which the ending function will wait |
retval | The optional return value of the thread |
abstime | The absolute timestamp after which the thread will have been ended |
|
staticprivate |
The helper function for the pthread_timedjoin_np() implementation.
threadData | The thread's data |
|
static |
Sets the priority of the current thread.
priority | Thread priority to set |
bool Ocean::Thread::shouldThreadStop | ( | ) | const |
|
static |
Sleeps the calling thread for a given time.
ms | Sleeping time in ms |
bool Ocean::Thread::startThread | ( | ) |
Starts the thread.
|
staticprivate |
Internal thread function calling the external thread function.
data | The data object which will contain the thread owner object |
|
staticprivate |
Internal thread function calling the external thread function.
data | The data object which will contain the thread owner object |
void Ocean::Thread::stopThread | ( | ) |
Informs the thread to stop.
see shouldThreadStop().
|
protected |
Tries to stop the thread gracefully.
However, if the thread can not be stopped it is terminated in a rough manner.
Call this function in the destructor of a derived class.
timeout | Time to wait for a graceful thread termination, in ms |
bool Ocean::Thread::terminateThread | ( | ) |
Terminates the thread.
Beware: The thread will be terminated in a very rough way.
|
static |
Returns the priority of the current thread.
|
protectedpure virtual |
This function has to be overloaded in derived class.
Implemented in Ocean::Tracking::RMV::RMVFeatureTracker6DOF, Ocean::Tracking::Offline::FrameTracker, Ocean::Tracking::MapTexturing::NewTextureGenerator, Ocean::Test::TestBase::TestSignal::AsyncFunctionThread, Ocean::Test::TestBase::TestSignal::SignalThread, Ocean::System::USB::Manager, Ocean::System::Memory::MemoryMeasurement, Ocean::Platform::WxWidgets::FrameMediumWindow, Ocean::Platform::Meta::Quest::PlatformSDK::Microphone, Ocean::Platform::Apple::MacOS::FrameMediumView, Ocean::Platform::Android::ProcessorMonitor, Ocean::Platform::Android::PowerMonitor, Ocean::Network::SocketScheduler, Ocean::Network::MaintenanceUDPConnector, Ocean::Network::MaintenanceTCPConnector, Ocean::Media::USB::USBLiveVideo, Ocean::Media::OpenImageLibraries::OILImageSequenceRecorder, Ocean::Media::MovieFrameProvider, Ocean::Media::ImageFileSequence, Ocean::Media::FFmpeg::FFMMovie, Ocean::Media::AVFoundation::AVFMovie, Ocean::Media::Android::AMovie, Ocean::Devices::SLAM::SLAMTracker6DOF, Ocean::Devices::RMV::RMVTracker6DOF, Ocean::Devices::Pattern::PatternTracker6DOF, Ocean::Devices::MapBuilding::OnDeviceRelocalizerTracker6DOF, Ocean::Devices::MapBuilding::OnDeviceMapCreatorTracker6DOF, Ocean::Devices::ARKit::AKFactory::GeoAnchorAvailabilityChecker, Ocean::Devices::Android::AndroidSensor::LooperManager, Ocean::ThreadPool, Ocean::ThreadPool::PoolThread, Ocean::Worker::WorkerThread, Ocean::TaskQueue, Ocean::Scheduler, and Ocean::EventManager.
|
static |
Waits until an object/variable has an expected value.
object | Reference to the object/variable whose value is to be checked |
expectedValue | The value that the object/variable is expected to have |
timeout | The optional timeout of this function, in seconds, with range [0, infinity), -1 to wait forever |
TObject | The data type of the object/variable to check |
TExpectedValue | The data type of the expected value |
|
static |
Waits until an object/variable has an expected value.
The provided temporary scoped lock will be repeatingly locked and released while the function is waiting and while accessing the object/variable.
The temporary scoped lock needs to be locked before calling this function, and it will also be locked when the function returns.
object | Reference to the object/variable whose value is to be checked |
expectedValue | The value that the object/variable is expected to have |
temporaryScopedLock | The temporary scoped lock which needs to be locked when calling the function; will be locked when the function returns |
timeout | The optional timeout of this function, in seconds, with range [0, infinity), -1 to wait forever |
TObject | The data type of the object/variable to check |
TExpectedValue | The data type of the expected value |
|
private |
Internal windows thread handle.
|
private |
Determines whether this thread is actually running.
|
private |
Determines whether this thread has been invoked to start immediately.
|
private |
Name of the thread.
|
private |
Internal pthread object.
|
private |
The seed value for random number generators.
|
private |
Determines whether this thread should stop.