MEL
Microthread & Execution library
mel::core::Thread Class Reference

Wrapper around std::thread to prive more abstractions. More...

#include <Thread.h>

Public Types

enum  YieldPolicy { YP_ANY_THREAD_ANY_PROCESSOR = 0 , YP_ANY_THREAD_SAME_PROCESSOR }
 

Public Member Functions

template<class F >
 Thread (F &&f)
 Starts the main thread routine. More...
 
virtual ~Thread ()
 Thread is joined on destruction.
 
void setPriority (EThreadPriority tp)
 
EThreadPriority getPriority () const
 
bool join (unsigned int millis=0xFFFFFFFF)
 
std::thread::id getThreadId () const
 
uint64_t getAffinity () const
 
bool setAffinity (uint64_t)
 
void terminate (unsigned int exitCode=0)
 

Static Public Member Functions

static void sleep (const unsigned int millis)
 
static void yield (YieldPolicy yp=YP_ANY_THREAD_SAME_PROCESSOR)
 
constexpr static unsigned getMinimunSleepTime ()
 

Detailed Description

Wrapper around std::thread to prive more abstractions.

Warning
Some of these features may not be present on certain platforms, or may have specific requirements.

Constructor & Destructor Documentation

◆ Thread()

template<class F >
mel::core::Thread::Thread ( F &&  f)

Starts the main thread routine.

Parameters
[in]ffuntion with signature void f()

Member Function Documentation

◆ getAffinity()

uint64_t mel::core::Thread::getAffinity ( ) const

Get thread state

Returns
the thread's current state returns if a terminate request is done (mEnd == true)

◆ getMinimunSleepTime()

constexpr static unsigned mel::core::Thread::getMinimunSleepTime ( )
inlinestaticconstexpr

return minimun time(msecs) the system will wait with accuracy. it depends on underlying OS and hardware, but traditionally it's about 10-15 msecs

◆ getPriority()

EThreadPriority mel::core::Thread::getPriority ( ) const
inline

Query thread's current priority.

Returns
the priority of the thread

◆ getThreadId()

std::thread::id mel::core::Thread::getThreadId ( ) const
inline

return handle for this thread

◆ join()

bool mel::core::Thread::join ( unsigned int  millis = 0xFFFFFFFF)

Forces the caller to wait for thread completion. Calling this method will cause the calling thread to be wait until Thread:run finishes or the timeout expires.

Parameters
millismaximum milliseconds to wait for the thread.
Returns
true if the thread finished. false if timeout

◆ setAffinity()

bool mel::core::Thread::setAffinity ( uint64_t  )

◆ setPriority()

void mel::core::Thread::setPriority ( EThreadPriority  tp)

Changes thread priority. May not be available in some platforms.

Parameters
tpthe new priority to be set.

◆ sleep()

static void mel::core::Thread::sleep ( const unsigned int  millis)
static

Forces the calling thread to sleep. Calling this method ensures the OS will schedule some CPU for any pending processes and threads that need attention.
NOTE: on some platforms, calling sleep(0) may cause the call to be completely ignored.

Parameters
millisthe number of milliseconds to sleep for. The actual sleep time may depend on each platform, but you can expect a granularity not finer than 10ms, meaning sleep(1) will make the thread sleep for almost the same time as sleep(10).

◆ terminate()

void mel::core::Thread::terminate ( unsigned int  exitCode = 0)

Forces thread termination Use with extreme caution; this method exists but it should rarely be used; it's always safer to invoke terminatRequest and wait for the Thread to terminate "naturally", instead of forcing it to quite.

Parameters
exitCodethe exit code for the terminated thread.

◆ yield()

static void mel::core::Thread::yield ( YieldPolicy  yp = YP_ANY_THREAD_SAME_PROCESSOR)
static

Forces the calling thread to yield execution. This may have slight different effects depending on the platform, but theoretically the execution is yielded only to threads in the same process as the caller.
Calling this method may not force the OS to schedule any CPU time for any pending processes different that the caller.


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