CppCommon  1.0.4.1
C++ Common Library
thread.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_THREADS_THREAD_H
10 #define CPPCOMMON_THREADS_THREAD_H
11 
13 #include "time/timestamp.h"
14 
15 #include <bitset>
16 #include <thread>
17 
19 
22 #define __THREAD__ CppCommon::Thread::CurrentThreadId()
23 
24 namespace CppCommon {
25 
27 enum class ThreadPriority : uint8_t
28 {
29  IDLE = 0x00,
30  LOWEST = 0x1F,
31  LOW = 0x3F,
32  NORMAL = 0x7F,
33  HIGH = 0x9F,
34  HIGHEST = 0xBF,
35  REALTIME = 0xFF
36 };
37 
39 
44 template <class TOutputStream>
45 TOutputStream& operator<<(TOutputStream& stream, ThreadPriority priority);
46 
48 
55 class Thread
56 {
57 public:
58  Thread() = delete;
59  Thread(const Thread&) = delete;
60  Thread(Thread&&) = delete;
61  ~Thread() = delete;
62 
63  Thread& operator=(const Thread&) = delete;
64  Thread& operator=(Thread&&) = delete;
65 
67 
70  static uint64_t CurrentThreadId() noexcept;
71 
73 
76  static uint32_t CurrentThreadAffinity() noexcept;
77 
79 
87  template <class Fn, class... Args>
88  static std::thread Start(Fn&& fn, Args&&... args);
89 
91 
94  static void Sleep(int64_t milliseconds) noexcept
95  { SleepFor(Timespan::milliseconds(milliseconds)); }
97 
100  static void SleepFor(const Timespan& timespan) noexcept;
102 
105  static void SleepUntil(const UtcTimestamp& timestamp) noexcept
106  { SleepFor(timestamp - UtcTimestamp()); }
107 
109  static void Yield() noexcept;
110 
112 
115  static std::bitset<64> GetAffinity();
117 
121  static std::bitset<64> GetAffinity(std::thread& thread);
122 
124 
127  static void SetAffinity(const std::bitset<64>& affinity);
129 
133  static void SetAffinity(std::thread& thread, const std::bitset<64>& affinity);
134 
136 
139  static ThreadPriority GetPriority();
141 
145  static ThreadPriority GetPriority(std::thread& thread);
146 
148 
151  static void SetPriority(ThreadPriority priority);
153 
157  static void SetPriority(std::thread& thread, ThreadPriority priority);
158 };
159 
162 } // namespace CppCommon
163 
164 #include "thread.inl"
165 
166 #endif // CPPCOMMON_THREADS_THREAD_H
Thread abstraction.
Definition: thread.h:56
Thread(const Thread &)=delete
static ThreadPriority GetPriority()
Get the current thread priority.
Definition: thread.cpp:298
Thread(Thread &&)=delete
static uint64_t CurrentThreadId() noexcept
Get the current thread Id.
Definition: thread.cpp:60
static void Sleep(int64_t milliseconds) noexcept
Sleep the current thread for the given milliseconds.
Definition: thread.h:94
static void SleepFor(const Timespan &timespan) noexcept
Sleep the current thread for the given timespan.
Definition: thread.cpp:83
static void SetPriority(ThreadPriority priority)
Set the current thread priority.
Definition: thread.cpp:398
static std::bitset< 64 > GetAffinity()
Get the current thread CPU affinity bitset.
Definition: thread.cpp:152
Thread & operator=(const Thread &)=delete
static void Yield() noexcept
Yield to other threads.
Definition: thread.cpp:143
static uint32_t CurrentThreadAffinity() noexcept
Get the current thread CPU affinity.
Definition: thread.cpp:71
static void SetAffinity(const std::bitset< 64 > &affinity)
Set the current thread CPU affinity bitset.
Definition: thread.cpp:254
static void SleepUntil(const UtcTimestamp &timestamp) noexcept
Sleep the current thread until the given timestamp.
Definition: thread.h:105
static std::thread Start(Fn &&fn, Args &&... args)
Start a new thread with an exception handler registered.
Definition: thread.inl:45
Thread & operator=(Thread &&)=delete
int64_t milliseconds() const noexcept
Get total milliseconds of the current timespan.
Definition: timespan.h:141
UTC timestamp.
Definition: timestamp.h:248
Exceptions handler definition.
C++ Common project definitions.
Definition: token_bucket.h:15
std::ostream & operator<<(std::ostream &os, const uint128_t &value)
Definition: uint128.inl:155
ThreadPriority
Thread priorities.
Definition: thread.h:28
@ NORMAL
Normal thread priority.
@ LOW
Low thread priority.
@ REALTIME
Realtime thread priority.
@ LOWEST
Lowest thread priority.
@ IDLE
Idle thread priority.
@ HIGH
High thread priority.
@ HIGHEST
Highest thread priority.
Timestamp definition.