libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::ConditionVar Class Reference

条件变量封装类 / Condition variable wrapper class More...

#include <condition_var.hpp>

Public Member Functions

 ConditionVar ()
 默认构造函数 / Default constructor
 
 ~ConditionVar ()
 析构函数 / Destructor
 
ErrorCode Wait (uint32_t timeout)
 等待条件变量 / Waits for the condition variable
 
void Signal ()
 发送信号唤醒一个等待线程 / Signals one waiting thread
 
void Broadcast ()
 广播信号唤醒所有等待线程 / Broadcasts a signal to wake up all waiting threads
 

Private Attributes

condition_var_handle handle_
 底层条件变量句柄 / Underlying condition variable handle
 

Detailed Description

条件变量封装类 / Condition variable wrapper class

该类提供了条件变量 (Condition Variable) 的封装,实现了线程间的同步机制, 允许一个线程等待某个条件发生,另一个线程通知该条件已满足。 This class provides a wrapper for condition variables, implementing a synchronization mechanism where one thread waits for a condition to occur while another thread signals the condition.

Definition at line 19 of file condition_var.hpp.

Constructor & Destructor Documentation

◆ ConditionVar()

ConditionVar::ConditionVar ( )

默认构造函数 / Default constructor

初始化条件变量的内部句柄。 Initializes the internal handle of the condition variable.

Definition at line 7 of file condition_var.cpp.

condition_var_handle handle_
底层条件变量句柄 / Underlying condition variable handle
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

◆ ~ConditionVar()

ConditionVar::~ConditionVar ( )

析构函数 / Destructor

释放条件变量资源。 Releases the condition variable resources.

Definition at line 10 of file condition_var.cpp.

Member Function Documentation

◆ Broadcast()

void ConditionVar::Broadcast ( )

广播信号唤醒所有等待线程 / Broadcasts a signal to wake up all waiting threads

该方法通知所有正在等待条件变量的线程,使它们继续执行。 This method notifies all threads waiting on the condition variable to resume execution.

Definition at line 22 of file condition_var.cpp.

22 {
23 while (xSemaphoreTake(handle_, 0) != pdTRUE) {
25 }
26
28}

◆ Signal()

void ConditionVar::Signal ( )

发送信号唤醒一个等待线程 / Signals one waiting thread

该方法通知一个正在等待条件变量的线程,使其继续执行。 This method notifies a single thread waiting on the condition variable to resume execution.

Definition at line 20 of file condition_var.cpp.

◆ Wait()

ErrorCode ConditionVar::Wait ( uint32_t  timeout)

等待条件变量 / Waits for the condition variable

Parameters
timeout等待的超时时间(毫秒) / Timeout in milliseconds
Returns
操作结果,成功返回 ErrorCode::OK,超时返回 ErrorCode::TIMEOUT / Operation result: returns ErrorCode::OK on success, ErrorCode::TIMEOUT if timed out

该方法会阻塞当前线程,直到收到信号或超时。 This method blocks the current thread until it receives a signal or the timeout occurs.

Definition at line 12 of file condition_var.cpp.

12 {
13 if (xSemaphoreTake(handle_, timeout) == pdTRUE) {
14 return ErrorCode::OK;
15 } else {
16 return ErrorCode::TIMEOUT;
17 }
18}

Field Documentation

◆ handle_

condition_var_handle LibXR::ConditionVar::handle_
private

底层条件变量句柄 / Underlying condition variable handle

Definition at line 23 of file condition_var.hpp.


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