18#include <shared_mutex>
19#include <condition_variable>
56 std::string
name{
"unnamed"};
89 template<
typename Func>
92 thread_([this, f = std::forward<Func>(func)]() mutable {
93 this->thread_function(std::move(f));
108 if (thread_.joinable()) {
122 template<
typename Func>
124 if (thread_.joinable()) {
127 thread_ = std::thread([
this, f = std::forward<Func>(func)]()
mutable {
128 this->thread_function(std::move(f));
136 if (thread_.joinable()) {
145 if (thread_.joinable()) {
154 return thread_.joinable();
161 return thread_.native_handle();
167 std::thread::id
get_id() const noexcept {
168 return thread_.get_id();
182 template<
typename Func>
183 void thread_function(Func&& func) {
186 if (!config_.name.empty()) {
187 pthread_setname_np(pthread_self(), config_.name.c_str());
192 apply_thread_config();
201 void apply_thread_config() {
202 pthread_t thread_handle = pthread_self();
208 int policy = SCHED_OTHER;
209 switch (config_.policy) {
217 policy = SCHED_OTHER;
220 struct sched_param param;
221 param.sched_priority =
static_cast<int>(config_.priority);
224 pthread_setschedparam(thread_handle, policy, ¶m);
228 if (config_.cpu_affinity >= 0) {
231 CPU_SET(config_.cpu_affinity, &cpuset);
232 pthread_setaffinity_np(thread_handle,
sizeof(cpu_set_t), &cpuset);
236 ThreadConfig config_;
286 std::shared_mutex&
native() {
return mutex_; }
289 std::shared_mutex mutex_;
302using Lock = std::lock_guard<Mutex>;
331 void wait(std::unique_lock<std::mutex>& lock) { cv_.wait(lock); }
333 template<
typename Predicate>
334 void wait(std::unique_lock<std::mutex>& lock, Predicate pred) {
335 cv_.wait(lock, pred);
338 template<
typename Rep,
typename Period>
339 std::cv_status
wait_for(std::unique_lock<std::mutex>& lock,
340 const std::chrono::duration<Rep, Period>& rel_time) {
341 return cv_.wait_for(lock, rel_time);
345 std::condition_variable cv_;
359#define Synchronized(mutex) \
360 if (commrat::Lock _lock_##__LINE__{mutex}; true)
371#define ReadLocked(mutex) \
372 if (commrat::SharedLock _lock_##__LINE__{mutex}; true)
383#define WriteLocked(mutex) \
384 if (commrat::UniqueLockShared _lock_##__LINE__{mutex}; true)
Condition variable wrapper.
void notify_all() noexcept
void notify_one() noexcept
ConditionVariable(const ConditionVariable &)=delete
ConditionVariable & operator=(const ConditionVariable &)=delete
ConditionVariable()=default
~ConditionVariable()=default
void wait(std::unique_lock< std::mutex > &lock)
void wait(std::unique_lock< std::mutex > &lock, Predicate pred)
std::cv_status wait_for(std::unique_lock< std::mutex > &lock, const std::chrono::duration< Rep, Period > &rel_time)
Mutex wrapper (future: realtime mutex support)
Mutex & operator=(const Mutex &)=delete
Mutex(const Mutex &)=delete
Shared mutex wrapper (reader-writer lock)
std::shared_mutex & native()
SharedMutex(const SharedMutex &)=delete
SharedMutex & operator=(const SharedMutex &)=delete
Thread wrapper with realtime support.
std::thread::native_handle_type native_handle()
Get native thread handle.
Thread & operator=(const Thread &)=delete
const ThreadConfig & config() const noexcept
Get thread configuration.
Thread(const ThreadConfig &config, Func &&func)
Create and start thread with function.
bool joinable() const noexcept
Check if thread is joinable.
void join()
Join thread (wait for completion)
std::thread::id get_id() const noexcept
Get thread ID.
Thread()=default
Default constructor - no thread running.
void start(Func &&func)
Start thread with function (if not already started)
void detach()
Detach thread.
~Thread()
Destructor - joins if joinable.
Thread(Thread &&)=default
Thread(const ThreadConfig &config)
Create thread without starting (call start() later)
Thread & operator=(Thread &&)=default
Thread(const Thread &)=delete
CommRaT - Modern C++ Real-Time Communication Framework.
std::unique_lock< Mutex > UniqueLock
SchedulingPolicy
Thread scheduling policy.
@ NORMAL
Default OS scheduling (SCHED_OTHER)
@ ROUND_ROBIN
Round-robin realtime (SCHED_RR)
@ FIFO
First-in-first-out realtime (SCHED_FIFO)
@ DEADLINE
Deadline scheduling (SCHED_DEADLINE) - future.
std::shared_lock< SharedMutex > SharedLock
Scoped shared lock (RAII) - for readers.
std::lock_guard< Mutex > Lock
Scoped lock guard (RAII)
std::unique_lock< SharedMutex > UniqueLockShared
Scoped unique lock (RAII) - for writers.
ThreadPriority
Thread priority levels.
@ NORMAL
Normal priority (default)
@ REALTIME
Realtime priority (critical paths)
@ IDLE
Idle priority (background tasks)
@ HIGH
High priority (I/O, event handling)
int cpu_affinity
-1 = no affinity, >= 0 = pin to CPU
size_t stack_size
0 = default, > 0 = custom stack size