libxr
1.0
Want to be the best embedded framework
|
线程管理类,封装 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 | |
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 (TimestampMS &last_waskup_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 | |
Private Attributes | |
libxr_thread_handle | thread_handle_ |
POSIX 线程句柄 POSIX thread handle. | |
线程管理类,封装 POSIX 线程创建和调度 Thread management class encapsulating POSIX thread creation and scheduling
Definition at line 13 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 20 of file thread.hpp.
|
inline |
默认构造函数,初始化空线程 Default constructor initializing an empty thread
Definition at line 34 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 41 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
需要进行调整以符合 POSIX 线程的栈管理方式。 如果系统支持 SCHED_RR
调度策略,则设置线程优先级。
This method creates a new thread using POSIX pthread_create()
, executing function
with arg
as the argument. The stack_depth
needs adjustment for POSIX thread stack management. If the system supports SCHED_RR
scheduling, thread priority is set accordingly.
线程数据封装类 Thread data encapsulation class
构造函数,存储线程相关数据 Constructor to store thread-related data
线程入口函数,执行用户定义的线程函数 Thread entry function that executes the user-defined function
arg | 线程参数 Thread argument |
nullptr
The return value is always nullptr
< 线程执行的函数 Function executed by the thread
< 线程函数的参数 Argument passed to the thread function
< 线程名称 Thread name
Definition at line 64 of file thread.hpp.
|
static |
获取当前线程对象 Gets the current thread object
Definition at line 14 of file thread.cpp.
|
static |
获取当前系统时间(毫秒) Gets the current system time in milliseconds
Definition at line 39 of file thread.cpp.
|
inline |
线程对象转换为 POSIX 线程句柄 Converts the thread object to a POSIX thread handle
Definition at line 216 of file thread.hpp.
|
static |
让线程进入休眠状态 Puts the thread to sleep
milliseconds | 休眠时间(毫秒) Sleep duration in milliseconds |
Definition at line 16 of file thread.cpp.
|
static |
让线程休眠直到指定时间点 Puts the thread to sleep until a specified time
last_waskup_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 47 of file thread.cpp.
|
private |
POSIX 线程句柄 POSIX thread handle.
Definition at line 219 of file thread.hpp.