|
libxr
Embedded C++ framework for bare-metal, RTOS and Linux
|
线程管理类,封装 POSIX 线程创建和调度 Thread management class encapsulating POSIX thread creation and scheduling More...
#include <thread.hpp>
Public Types | |
| enum class | Priority : uint8_t { IDLE , LOW , MEDIUM , HIGH , REALTIME , NUMBER } |
| 线程优先级枚举 Enumeration for thread priorities More... | |
Public Member Functions | |
| Thread () | |
| 默认构造函数,初始化空线程 Default constructor initializing an empty thread | |
| Thread (libxr_thread_handle handle) | |
| 通过 POSIX 线程句柄创建线程对象 Constructor to create a thread object from a POSIX thread handle | |
| template<typename ArgType > | |
| void | Create (ArgType arg, void(*function)(ArgType arg), const char *name, size_t stack_depth, Thread::Priority priority) |
| 创建新线程 Creates a new thread | |
| ErrorCode | Join () |
| Waits for this POSIX thread to terminate and releases its resources. | |
| operator libxr_thread_handle () | |
| 线程对象转换为 POSIX 线程句柄 Converts the thread object to a POSIX thread handle | |
Static Public Member Functions | |
| static Thread | Current (void) |
| 获取当前线程对象 Gets the current thread object | |
| static uint32_t | GetTime () |
| 获取当前系统时间(毫秒) Gets the current system time in milliseconds | |
| static void | Sleep (uint32_t milliseconds) |
| 让线程进入休眠状态 Puts the thread to sleep | |
| static void | SleepUntil (MillisecondTimestamp &last_wakeup_time, uint32_t time_to_sleep) |
| 让线程休眠直到指定时间点 Puts the thread to sleep until a specified time | |
| static void | Yield () |
| 让出 CPU 以执行其他线程 Yields CPU execution to allow other threads to run | |
线程管理类,封装 POSIX 线程创建和调度 Thread management class encapsulating POSIX thread creation and scheduling
Definition at line 18 of file thread.hpp.
|
strong |
线程优先级枚举 Enumeration for thread priorities
| Enumerator | |
|---|---|
| IDLE | 空闲优先级 / Idle priority |
| LOW | 低优先级 / Low priority |
| MEDIUM | 中等优先级 / Medium priority |
| HIGH | 高优先级 / High priority |
| REALTIME | 实时优先级 / Realtime priority |
| NUMBER | 优先级数量 / Number of priority levels |
Definition at line 25 of file thread.hpp.
|
inline |
默认构造函数,初始化空线程 Default constructor initializing an empty thread
Definition at line 39 of file thread.hpp.
|
inline |
通过 POSIX 线程句柄创建线程对象 Constructor to create a thread object from a POSIX thread handle
| handle | POSIX 线程句柄 / POSIX thread handle |
Definition at line 46 of file thread.hpp.
|
inline |
创建新线程 Creates a new thread
| ArgType | 线程函数的参数类型 / The type of argument for the thread function |
| arg | 线程函数的参数 / Argument for the thread function |
| function | 线程执行的函数 / Function executed by the thread |
| name | 线程名称 / Thread name |
| stack_depth | 线程栈大小(字节) / Stack size of the thread (bytes) |
| priority | 线程优先级 / Thread priority |
该方法基于 POSIX pthread_create() 创建新线程,执行 function 并传递 arg 作为参数。栈大小取 stack_depth 与 PTHREAD_STACK_MIN 中的较大值。SCHED_FIFO 的优先级范围足够时,线程使用 SCHED_FIFO 策略,优先级为最低优先级加 priority; 范围不足或设置优先级失败时使用默认调度策略。 按这些属性创建失败时,改用默认属性重试一次;进程内第一次重试时输出一条警告。
This method creates a new thread using POSIX pthread_create(), executing function with arg as the argument. The stack size is the larger of stack_depth and PTHREAD_STACK_MIN. When the SCHED_FIFO priority range is large enough, the thread uses SCHED_FIFO with the minimum priority plus priority; otherwise, or when setting the priority fails, it uses the default policy. If creation with these attributes fails, it is retried once with default attributes; the first such retry in the process logs a warning.
Definition at line 74 of file thread.hpp.
|
static |
获取当前线程对象 Gets the current thread object
Definition at line 13 of file thread.cpp.
|
static |
获取当前系统时间(毫秒) Gets the current system time in milliseconds
Definition at line 35 of file thread.cpp.
| ErrorCode Thread::Join | ( | ) |
Waits for this POSIX thread to terminate and releases its resources.
OK on success, otherwise FAILED. Definition at line 39 of file thread.cpp.
|
inline |
线程对象转换为 POSIX 线程句柄 Converts the thread object to a POSIX thread handle
Definition at line 191 of file thread.hpp.
|
static |
让线程进入休眠状态 Puts the thread to sleep
| milliseconds | 休眠时间(毫秒) / Sleep duration in milliseconds |
Definition at line 15 of file thread.cpp.
|
static |
让线程休眠直到指定时间点 Puts the thread to sleep until a specified time
| last_wakeup_time | 上次唤醒时间 / Last wake-up time |
| time_to_sleep | 休眠时长(毫秒) / Sleep duration in milliseconds |
Definition at line 23 of file thread.cpp.
|
static |
让出 CPU 以执行其他线程 Yields CPU execution to allow other threads to run
Definition at line 37 of file thread.cpp.