CppCommon  1.0.4.1
C++ Common Library
Public Member Functions | Static Public Member Functions | List of all members
CppCommon::Thread Class Reference

Thread abstraction. More...

#include <thread.h>

Public Member Functions

 Thread ()=delete
 
 Thread (const Thread &)=delete
 
 Thread (Thread &&)=delete
 
 ~Thread ()=delete
 
Threadoperator= (const Thread &)=delete
 
Threadoperator= (Thread &&)=delete
 

Static Public Member Functions

static uint64_t CurrentThreadId () noexcept
 Get the current thread Id. More...
 
static uint32_t CurrentThreadAffinity () noexcept
 Get the current thread CPU affinity. More...
 
template<class Fn , class... Args>
static std::thread Start (Fn &&fn, Args &&... args)
 Start a new thread with an exception handler registered. More...
 
static void Sleep (int64_t milliseconds) noexcept
 Sleep the current thread for the given milliseconds. More...
 
static void SleepFor (const Timespan &timespan) noexcept
 Sleep the current thread for the given timespan. More...
 
static void SleepUntil (const UtcTimestamp &timestamp) noexcept
 Sleep the current thread until the given timestamp. More...
 
static void Yield () noexcept
 Yield to other threads. More...
 
static std::bitset< 64 > GetAffinity ()
 Get the current thread CPU affinity bitset. More...
 
static std::bitset< 64 > GetAffinity (std::thread &thread)
 Get the given thread CPU affinity bitset. More...
 
static void SetAffinity (const std::bitset< 64 > &affinity)
 Set the current thread CPU affinity bitset. More...
 
static void SetAffinity (std::thread &thread, const std::bitset< 64 > &affinity)
 Set the given thread CPU affinity bitset. More...
 
static ThreadPriority GetPriority ()
 Get the current thread priority. More...
 
static ThreadPriority GetPriority (std::thread &thread)
 Get the given thread priority. More...
 
static void SetPriority (ThreadPriority priority)
 Set the current thread priority. More...
 
static void SetPriority (std::thread &thread, ThreadPriority priority)
 Set the given thread priority. More...
 

Detailed Description

Thread abstraction.

Thread contains different kinds of thread manipulation functionality such as retrive the current thread Id, sleep for the given time period in nanoseconds, etc.

Thread-safe.

Definition at line 55 of file thread.h.

Constructor & Destructor Documentation

◆ Thread() [1/3]

CppCommon::Thread::Thread ( )
delete

◆ Thread() [2/3]

CppCommon::Thread::Thread ( const Thread )
delete

◆ Thread() [3/3]

CppCommon::Thread::Thread ( Thread &&  )
delete

◆ ~Thread()

CppCommon::Thread::~Thread ( )
delete

Member Function Documentation

◆ CurrentThreadAffinity()

uint32_t CppCommon::Thread::CurrentThreadAffinity ( )
staticnoexcept

Get the current thread CPU affinity.

Returns
Current thread CPU affinity
Examples
threads_thread.cpp.

Definition at line 71 of file thread.cpp.

◆ CurrentThreadId()

uint64_t CppCommon::Thread::CurrentThreadId ( )
staticnoexcept

Get the current thread Id.

Returns
Current thread Id
Examples
threads_thread.cpp.

Definition at line 60 of file thread.cpp.

◆ GetAffinity() [1/2]

std::bitset< 64 > CppCommon::Thread::GetAffinity ( )
static

Get the current thread CPU affinity bitset.

Returns
CPU affinity bitset of the current thread

Definition at line 152 of file thread.cpp.

◆ GetAffinity() [2/2]

std::bitset< 64 > CppCommon::Thread::GetAffinity ( std::thread &  thread)
static

Get the given thread CPU affinity bitset.

Parameters
thread- Thread
Returns
CPU affinity bitset of the given thread

Definition at line 203 of file thread.cpp.

◆ GetPriority() [1/2]

ThreadPriority CppCommon::Thread::GetPriority ( )
static

Get the current thread priority.

Returns
Priority of the current thread

Definition at line 298 of file thread.cpp.

◆ GetPriority() [2/2]

ThreadPriority CppCommon::Thread::GetPriority ( std::thread &  thread)
static

Get the given thread priority.

Parameters
thread- Thread
Returns
Priority of the given thread

Definition at line 348 of file thread.cpp.

◆ operator=() [1/2]

Thread& CppCommon::Thread::operator= ( const Thread )
delete

◆ operator=() [2/2]

Thread& CppCommon::Thread::operator= ( Thread &&  )
delete

◆ SetAffinity() [1/2]

void CppCommon::Thread::SetAffinity ( const std::bitset< 64 > &  affinity)
static

Set the current thread CPU affinity bitset.

Parameters
affinity- Thread CPU affinity bitset
Examples
threads_thread.cpp.

Definition at line 254 of file thread.cpp.

◆ SetAffinity() [2/2]

void CppCommon::Thread::SetAffinity ( std::thread &  thread,
const std::bitset< 64 > &  affinity 
)
static

Set the given thread CPU affinity bitset.

Parameters
thread- Thread
affinity- Thread CPU affinity bitset

Definition at line 276 of file thread.cpp.

◆ SetPriority() [1/2]

void CppCommon::Thread::SetPriority ( std::thread &  thread,
ThreadPriority  priority 
)
static

Set the given thread priority.

Parameters
thread- Thread
priority- Thread priority

Definition at line 464 of file thread.cpp.

◆ SetPriority() [2/2]

void CppCommon::Thread::SetPriority ( ThreadPriority  priority)
static

Set the current thread priority.

Parameters
priority- Thread priority

Definition at line 398 of file thread.cpp.

◆ Sleep()

static void CppCommon::Thread::Sleep ( int64_t  milliseconds)
inlinestaticnoexcept

Sleep the current thread for the given milliseconds.

Parameters
milliseconds- Milliseconds to sleep
Examples
threads_thread.cpp.

Definition at line 94 of file thread.h.

◆ SleepFor()

void CppCommon::Thread::SleepFor ( const Timespan timespan)
staticnoexcept

◆ SleepUntil()

static void CppCommon::Thread::SleepUntil ( const UtcTimestamp timestamp)
inlinestaticnoexcept

Sleep the current thread until the given timestamp.

Parameters
timestamp- Timestamp to stop sleeping

Definition at line 105 of file thread.h.

◆ Start()

template<class Fn , class... Args>
std::thread CppCommon::Thread::Start ( Fn &&  fn,
Args &&...  args 
)
inlinestatic

Start a new thread with an exception handler registered.

Works the same way as std::thread() does but also register an exception handler with ExceptionsHandler.SetupThread() call.

Parameters
fn- Thread function
args- Thread function arguments
Returns
Instance of a new thread
Examples
errors_exceptions_handler.cpp, and threads_thread.cpp.

Definition at line 45 of file thread.inl.

◆ Yield()

void CppCommon::Thread::Yield ( )
staticnoexcept

Yield to other threads.

Examples
algorithms_token_bucket.cpp.

Definition at line 143 of file thread.cpp.


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