跳转至

pthread

pthread (POSIX Threads) 提供符合 POSIX 标准的线程管理功能,支持线程创建、同步、调度及线程特定数据操作。

头文件清单

#include "open_source/musl/include/pthread.h"

接口清单

接口名称 功能简述
pthread_attr_destroy 删除线程的属性
pthread_attr_getdetachstate 获取脱离状态的属性
pthread_attr_getinheritsched 获取任务调度方式
pthread_attr_getschedparam 获取任务调度参数
pthread_attr_getschedpolicy 获取任务调度策略属性
pthread_attr_getscope 获取任务范围属性
pthread_attr_getstackaddr 获取任务堆栈的起始地址
pthread_attr_getstacksize 获取任务属性堆栈大小
pthread_attr_init 初始化任务属性
pthread_attr_setdetachstate 设置任务属性分离状态
pthread_attr_setinheritsched 设置任务调度方式
pthread_attr_setschedparam 设置任务调度参数
pthread_attr_setschedpolicy 设置任务调度策略属性
pthread_attr_setscope 设置任务范围属性
pthread_attr_setstackaddr 设置任务堆栈的起始地址
pthread_attr_setstacksize 设置任务属性堆栈大小
pthread_cancel 取消任务
pthread_cond_broadcast 唤醒所有被阻塞在条件变量上的线程
pthread_cond_destroy 释放条件变量
pthread_cond_init 初始化条件变量
pthread_cond_signal 释放被阻塞在条件变量上的一个线程
pthread_cond_timedwait 超时时限内等待一个条件变量
pthread_cond_wait 等待一个条件变量
pthread_condattr_destroy 删除存储并使属性对象无效
pthread_condattr_getclock 获取任务时钟
pthread_condattr_getpshared 获取条件变量属性
pthread_condattr_init 初始化条件变量属性
pthread_condattr_setclock 设置任务时钟
pthread_condattr_setpshared 设置条件变量属性
pthread_create 创建任务
pthread_detach 分离任务
pthread_equal 判断是否为同一任务
pthread_exit 任务退出
pthread_getschedparam 获取任务优先级及调度参数
pthread_getspecific 获取调用线程的键绑定
pthread_join 阻塞任务
pthread_key_create 分配用于标识进程中线程特定数据的键
pthread_key_delete 销毁现有线程特定数据键
pthread_mutex_destroy 删除互斥锁
pthread_mutex_getprioceiling 获取互斥锁的优先级上限
pthread_mutex_init 初始化互斥锁
pthread_mutex_lock 申请互斥锁(阻塞操作)
pthread_mutex_setprioceiling 设置互斥锁的优先级上限
pthread_mutex_timedlock 申请互斥锁(只在设定时间内阻塞)
pthread_mutex_trylock 尝试申请互斥锁(非阻塞)
pthread_mutex_unlock 释放互斥锁
pthread_mutexattr_destroy 销毁互斥锁属性对象
pthread_mutexattr_getprioceiling 获取互斥锁属性的优先级上限
pthread_mutexattr_getprotocol 获取互斥锁属性的协议属性
pthread_mutexattr_gettype 获取互斥锁的类型属性
pthread_mutexattr_init 初始化互斥锁属性对象
pthread_mutexattr_setprioceiling 设置互斥锁属性的优先级上限
pthread_mutexattr_setprotocol 设置互斥锁属性的协议属性
pthread_mutexattr_settype 设置互斥锁的类型属性
pthread_once 一次性操作任务
pthread_self 获取任务ID
pthread_setcancelstate 任务cancel功能开关
pthread_setcanceltype 设置任务cancel类型
pthread_setschedparam 设置任务优先级及调度策略
pthread_setschedprio 设置任务优先级
pthread_setspecific 设置线程数据
pthread_testcancel cancel任务

Functions

pthread_attr_destroy

int pthread_attr_destroy(pthread_attr_t *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 销毁线程属性对象
  • 释放属性对象占用的资源
  • 属性对象销毁后不可再使用,如需使用需重新初始化

入参

名称 参数类型 详细说明 约束取值范围
attr pthread_attr_t * 指向待销毁的线程属性对象的指针 非NULL

返回值

返回值 文字含义 触发场景
0 成功 属性对象销毁成功
Other 其他错误码 执行失败

pthread_attr_getdetachstate

int pthread_attr_getdetachstate(const pthread_attr_t *, int *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 获取线程属性中的分离状态属性
  • 通过出参返回分离状态值
  • 分离状态决定线程结束时是否自动回收资源

入参

名称 参数类型 详细说明 约束取值范围
attr const pthread_attr_t * 指向线程属性对象的指针 非NULL

出参

名称 数据类型 输出说明
detachstate int 分离状态值

返回值

返回值 文字含义 触发场景
0 成功 获取分离状态成功
Other 其他错误码 执行失败

pthread_attr_getinheritsched

int pthread_attr_getinheritsched(const pthread_attr_t *__restrict, int *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 获取线程属性中的调度继承方式
  • 通过出参返回调度继承属性值
  • 调度继承方式决定新线程是否继承父线程的调度参数

入参

名称 参数类型 详细说明 约束取值范围
attr const pthread_attr_t *__restrict 指向线程属性对象的指针 非NULL

出参

名称 数据类型 输出说明
inheritsched int 调度继承方式

返回值

返回值 文字含义 触发场景
0 成功 获取调度继承方式成功
Other 其他错误码 执行失败

pthread_attr_getschedparam

int pthread_attr_getschedparam(const pthread_attr_t *__restrict, struct sched_param *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 获取线程属性中的调度参数
  • 通过出参返回调度参数(含优先级等)
  • 调度参数用于控制线程的调度行为

入参

名称 参数类型 详细说明 约束取值范围
attr const pthread_attr_t *__restrict 指向线程属性对象的指针 非NULL

出参

名称 数据类型 输出说明
param struct sched_param 调度参数,包含优先级等信息

返回值

返回值 文字含义 触发场景
0 成功 获取调度参数成功
Other 其他错误码 执行失败

pthread_attr_getschedpolicy

int pthread_attr_getschedpolicy(const pthread_attr_t *__restrict, int *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 获取线程属性中的调度策略
  • 目前仅支持SCHED_RR调度策略,不支持SCHED_OTHER、SCHED_FIFO
  • 通过出参返回调度策略值

入参

名称 参数类型 详细说明 约束取值范围
attr const pthread_attr_t *__restrict 指向线程属性对象的指针 非NULL

出参

名称 数据类型 输出说明
policy int 调度策略值

返回值

返回值 文字含义 触发场景
0 成功 获取调度策略成功
Other 其他错误码 执行失败

pthread_attr_getscope

int pthread_attr_getscope(const pthread_attr_t *__restrict, int *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 获取线程属性中的竞争范围
  • 任务使用范围目前只支持PTHREAD_SCOPE_SYSTEM,不支持PTHREAD_SCOPE_PROCESS
  • 通过出参返回竞争范围值

入参

名称 参数类型 详细说明 约束取值范围
attr const pthread_attr_t *__restrict 指向线程属性对象的指针 非NULL

出参

名称 数据类型 输出说明
scope int 竞争范围值

返回值

返回值 文字含义 触发场景
0 成功 获取竞争范围成功
Other 其他错误码 执行失败

pthread_attr_getstackaddr

int pthread_attr_getstackaddr(const pthread_attr_t *, void **)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 获取线程属性中的堆栈起始地址
  • 通过出参返回堆栈起始地址
  • 仅在__LITEOS__宏定义下可用

入参

名称 参数类型 详细说明 约束取值范围
attr const pthread_attr_t * 指向线程属性对象的指针 非NULL

出参

名称 数据类型 输出说明
stackaddr void ** 堆栈起始地址

返回值

返回值 文字含义 触发场景
0 成功 获取堆栈起始地址成功
Other 其他错误码 执行失败

pthread_attr_getstacksize

int pthread_attr_getstacksize(const pthread_attr_t *__restrict, size_t *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 获取线程属性中的堆栈大小
  • 通过出参返回堆栈大小值
  • 堆栈大小以字节为单位

入参

名称 参数类型 详细说明 约束取值范围
attr const pthread_attr_t *__restrict 指向线程属性对象的指针 非NULL

出参

名称 数据类型 输出说明
stacksize size_t 堆栈大小,单位字节

返回值

返回值 文字含义 触发场景
0 成功 获取堆栈大小成功
Other 其他错误码 执行失败

pthread_attr_init

int pthread_attr_init(pthread_attr_t *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 初始化线程属性对象
  • 将属性对象设置为默认值
  • 初始化后的属性对象可用于pthread_create创建线程

入参

名称 参数类型 详细说明 约束取值范围
attr pthread_attr_t * 指向待初始化的线程属性对象的指针 非NULL

返回值

返回值 文字含义 触发场景
0 成功 属性对象初始化成功
Other 其他错误码 执行失败

pthread_attr_setdetachstate

int pthread_attr_setdetachstate(pthread_attr_t *, int)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 设置线程属性中的分离状态
  • 分离状态决定线程结束时资源是否自动回收
  • 设置为PTHREAD_CREATE_DETACHED后线程结束自动回收资源,设置为PTHREAD_CREATE_JOINABLE需由其他线程join回收

入参

名称 参数类型 详细说明 约束取值范围
attr pthread_attr_t * 指向线程属性对象的指针 非NULL
detachstate int 分离状态值 - PTHREAD_CREATE_JOINABLE
- PTHREAD_CREATE_DETACHED

返回值

返回值 文字含义 触发场景
0 成功 设置分离状态成功
Other 其他错误码 执行失败

pthread_attr_setinheritsched

int pthread_attr_setinheritsched(pthread_attr_t *, int)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 设置线程属性中的调度继承方式
  • 调度继承方式决定新线程是否继承父线程的调度参数
  • 需将inheritsched设置为PTHREAD_EXPLICIT_SCHED以使自定义调度参数生效

入参

名称 参数类型 详细说明 约束取值范围
attr pthread_attr_t * 指向线程属性对象的指针 非NULL
inheritsched int 调度继承方式 - PTHREAD_INHERIT_SCHED
- PTHREAD_EXPLICIT_SCHED

返回值

返回值 文字含义 触发场景
0 成功 设置调度继承方式成功
Other 其他错误码 执行失败

pthread_attr_setschedparam

int pthread_attr_setschedparam(pthread_attr_t *__restrict, const struct sched_param *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 设置线程属性中的调度参数
  • 注意在LiteOS中,任务优先级的值越小,任务在系统中的优先级就越高,与标准库函数相反
  • 优先级仅支持0~31

前置条件

  • 需将pthread_attr_t任务属性的inheritsched字段设置为PTHREAD_EXPLICIT_SCHED,否则设置的任务调度优先级将不会生效
  • 系统默认设置为PTHREAD_INHERIT_SCHED

入参

名称 参数类型 详细说明 约束取值范围
attr pthread_attr_t *__restrict 指向线程属性对象的指针 非NULL
param const struct sched_param *__restrict 调度参数,包含优先级等信息 非NULL,sched_priority范围0~31

返回值

返回值 文字含义 触发场景
0 成功 设置调度参数成功
Other 其他错误码 执行失败

pthread_attr_setschedpolicy

int pthread_attr_setschedpolicy(pthread_attr_t *, int)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 设置线程属性中的调度策略
  • 目前仅支持SCHED_RR调度策略,不支持SCHED_OTHER、SCHED_FIFO
  • 调度策略决定线程的调度行为

入参

名称 参数类型 详细说明 约束取值范围
attr pthread_attr_t * 指向线程属性对象的指针 非NULL
policy int 调度策略值 SCHED_RR

返回值

返回值 文字含义 触发场景
0 成功 设置调度策略成功
Other 其他错误码 执行失败

pthread_attr_setscope

int pthread_attr_setscope(pthread_attr_t *, int)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 设置线程属性中的竞争范围
  • 任务使用范围目前只支持PTHREAD_SCOPE_SYSTEM,不支持PTHREAD_SCOPE_PROCESS
  • 竞争范围决定线程与系统中其他线程的竞争方式

入参

名称 参数类型 详细说明 约束取值范围
attr pthread_attr_t * 指向线程属性对象的指针 非NULL
scope int 竞争范围值 - PTHREAD_SCOPE_SYSTEM
- PTHREAD_SCOPE_PROCESS

返回值

返回值 文字含义 触发场景
0 成功 设置竞争范围成功
Other 其他错误码 执行失败

pthread_attr_setstackaddr

int pthread_attr_setstackaddr(pthread_attr_t *, void *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 设置线程属性中的堆栈起始地址
  • 仅在__LITEOS__宏定义下可用
  • 设置后创建的线程将使用指定地址作为堆栈起始地址

入参

名称 参数类型 详细说明 约束取值范围
attr pthread_attr_t * 指向线程属性对象的指针 非NULL
stackaddr void * 堆栈起始地址 非NULL

返回值

返回值 文字含义 触发场景
0 成功 设置堆栈起始地址成功
Other 其他错误码 执行失败

pthread_attr_setstacksize

int pthread_attr_setstacksize(pthread_attr_t *, size_t)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 设置线程属性中的堆栈大小
  • 堆栈大小以字节为单位
  • 堆栈大小需满足线程运行所需的最小空间

入参

名称 参数类型 详细说明 约束取值范围
attr pthread_attr_t * 指向线程属性对象的指针 非NULL
stacksize size_t 堆栈大小,单位字节 大于0

返回值

返回值 文字含义 触发场景
0 成功 设置堆栈大小成功
Other 其他错误码 执行失败

pthread_cancel

int pthread_cancel(pthread_t)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 请求取消指定线程
  • 仅支持先设置PTHREAD_CANCEL_ASYNCHRONOUS状态,再调用pthread_cancel取消任务
  • 取消请求的生效取决于目标线程的取消状态和类型

入参

名称 参数类型 详细说明 约束取值范围
thread pthread_t 待取消的线程ID 有效的线程ID

返回值

返回值 文字含义 触发场景
0 成功 取消请求已发送
Other 其他错误码 执行失败

pthread_cond_broadcast

int pthread_cond_broadcast(pthread_cond_t *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 唤醒所有被阻塞在指定条件变量上的线程
  • 若无线程等待,则函数无效果

入参

名称 参数类型 详细说明 约束取值范围
cond pthread_cond_t * 指向条件变量的指针 非NULL,已初始化

返回值

返回值 文字含义 触发场景
0 成功 唤醒操作成功
Other 其他错误码 执行失败

pthread_cond_destroy

int pthread_cond_destroy(pthread_cond_t *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 销毁条件变量
  • 释放条件变量占用的资源
  • 销毁后不可再使用该条件变量,如需使用需重新初始化

入参

名称 参数类型 详细说明 约束取值范围
cond pthread_cond_t * 指向待销毁的条件变量的指针 非NULL

返回值

返回值 文字含义 触发场景
0 成功 条件变量销毁成功
Other 其他错误码 执行失败

pthread_cond_init

int pthread_cond_init(pthread_cond_t *__restrict, const pthread_condattr_t *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 初始化条件变量
  • 可通过属性对象设置条件变量的属性
  • 若attr为NULL则使用默认属性

入参

名称 参数类型 详细说明 约束取值范围
cond pthread_cond_t *__restrict 指向待初始化的条件变量的指针 非NULL
attr const pthread_condattr_t *__restrict 条件变量属性对象指针 NULL或指向已初始化的属性对象

返回值

返回值 文字含义 触发场景
0 成功 条件变量初始化成功
Other 其他错误码 执行失败

pthread_cond_signal

int pthread_cond_signal(pthread_cond_t *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 唤醒至少一个被阻塞在指定条件变量上的线程
  • 若无线程等待,则函数无效果
  • 适用于只有一个线程需要被唤醒的场景

入参

名称 参数类型 详细说明 约束取值范围
cond pthread_cond_t * 指向条件变量的指针 非NULL,已初始化

返回值

返回值 文字含义 触发场景
0 成功 唤醒操作成功
Other 其他错误码 执行失败

pthread_cond_timedwait

int pthread_cond_timedwait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict, const struct timespec *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 在指定时间内等待条件变量
  • 当超时等待时间为相对时间时,LiteOS不能处理早已超时的情况
  • 超时或条件变量被唤醒时返回

前置条件

  • 调用时序约束:调用前必须持有mutex互斥锁
  • 条件变量和互斥锁均已初始化

入参

名称 参数类型 详细说明 约束取值范围
cond pthread_cond_t *__restrict 指向条件变量的指针 非NULL,已初始化
mutex pthread_mutex_t *__restrict 指向关联的互斥锁的指针 非NULL,已锁定
abstime const struct timespec *__restrict 超时时间 非NULL

返回值

返回值 文字含义 触发场景
0 成功 条件变量被唤醒
ETIMEDOUT 超时 等待超时
Other 其他错误码 执行失败

pthread_cond_wait

int pthread_cond_wait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 阻塞等待条件变量
  • 调用时自动释放mutex,被唤醒时重新获取mutex
  • 可能存在虚假唤醒,需在循环中使用

前置条件

  • 调用时序约束:调用前必须持有mutex互斥锁
  • 条件变量和互斥锁均已初始化

入参

名称 参数类型 详细说明 约束取值范围
cond pthread_cond_t *__restrict 指向条件变量的指针 非NULL,已初始化
mutex pthread_mutex_t *__restrict 指向关联的互斥锁的指针 非NULL,已锁定

返回值

返回值 文字含义 触发场景
0 成功 条件变量被唤醒
Other 其他错误码 执行失败

pthread_condattr_destroy

int pthread_condattr_destroy(pthread_condattr_t *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 销毁条件变量属性对象
  • 释放属性对象占用的资源
  • 属性对象销毁后不可再使用

入参

名称 参数类型 详细说明 约束取值范围
attr pthread_condattr_t * 指向待销毁的条件变量属性对象的指针 非NULL

返回值

返回值 文字含义 触发场景
0 成功 属性对象销毁成功
Other 其他错误码 执行失败

pthread_condattr_getclock

int pthread_condattr_getclock(const pthread_condattr_t *__restrict, clockid_t *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 获取条件变量属性中的时钟类型
  • 通过出参返回时钟ID
  • 时钟类型影响pthread_cond_timedwait的超时计算基准

入参

名称 参数类型 详细说明 约束取值范围
attr const pthread_condattr_t *__restrict 指向条件变量属性对象的指针 非NULL

出参

名称 数据类型 输出说明
clock_id clockid_t 时钟类型

返回值

返回值 文字含义 触发场景
0 成功 获取时钟类型成功
Other 其他错误码 执行失败

pthread_condattr_getpshared

int pthread_condattr_getpshared(const pthread_condattr_t *__restrict, int *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 获取条件变量属性中的进程共享属性
  • 目前只支持获取PTHREAD_PROCESS_PRIVATE条件变量属性
  • 通过出参返回进程共享属性值

入参

名称 参数类型 详细说明 约束取值范围
attr const pthread_condattr_t *__restrict 指向条件变量属性对象的指针 非NULL

出参

名称 数据类型 输出说明
pshared int 进程共享属性值

返回值

返回值 文字含义 触发场景
0 成功 获取进程共享属性成功
Other 其他错误码 执行失败

pthread_condattr_init

int pthread_condattr_init(pthread_condattr_t *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 初始化条件变量属性对象
  • 将属性对象设置为默认值
  • 初始化后的属性对象可用于pthread_cond_init

入参

名称 参数类型 详细说明 约束取值范围
attr pthread_condattr_t * 指向待初始化的条件变量属性对象的指针 非NULL

返回值

返回值 文字含义 触发场景
0 成功 属性对象初始化成功
Other 其他错误码 执行失败

pthread_condattr_setclock

int pthread_condattr_setclock(pthread_condattr_t *, clockid_t)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 设置条件变量属性中的时钟类型
  • 只支持CLOCK_MONOTONIC或CLOCK_REALTIME模式
  • 时钟类型影响pthread_cond_timedwait的超时计算基准

入参

名称 参数类型 详细说明 约束取值范围
attr pthread_condattr_t * 指向条件变量属性对象的指针 非NULL
clock_id clockid_t 时钟ID CLOCK_MONOTONIC、CLOCK_REALTIME

返回值

返回值 文字含义 触发场景
0 成功 设置时钟类型成功
Other 其他错误码 执行失败

pthread_condattr_setpshared

int pthread_condattr_setpshared(pthread_condattr_t *, int)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 设置条件变量属性中的进程共享属性
  • 只支持PTHREAD_PROCESS_PRIVATE属性
  • 进程共享属性决定条件变量是否可跨进程使用

入参

名称 参数类型 详细说明 约束取值范围
attr pthread_condattr_t * 指向条件变量属性对象的指针 非NULL
pshared int 进程共享属性值 - PTHREAD_PROCESS_PRIVATE
- PTHREAD_PROCESS_SHARED

返回值

返回值 文字含义 触发场景
0 成功 设置进程共享属性成功
Other 其他错误码 执行失败

pthread_create

int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 创建新线程
  • 新线程从start_routine函数开始执行
  • 可通过attr参数设置线程属性,attr为NULL时使用默认属性

入参

名称 参数类型 详细说明 约束取值范围
attr const pthread_attr_t *__restrict 线程属性对象指针 NULL或指向已初始化的属性对象
start_routine void ()(void *) 线程入口函数指针 非NULL
arg void *__restrict 传递给线程入口函数的参数 无限制

出参

名称 数据类型 输出说明
thread pthread_t 新创建的线程ID

返回值

返回值 文字含义 触发场景
0 成功 线程创建成功
Other 其他错误码 执行失败

pthread_detach

int pthread_detach(pthread_t)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 将指定线程设置为分离状态
  • 分离状态的线程结束后系统自动回收资源
  • 线程一旦被设置为分离状态,不可再被pthread_join等待

入参

名称 参数类型 详细说明 约束取值范围
thread pthread_t 待分离的线程ID 有效的线程ID

返回值

返回值 文字含义 触发场景
0 成功 线程分离成功
Other 其他错误码 执行失败

pthread_equal

int pthread_equal(pthread_t, pthread_t)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 判断两个线程ID是否为同一任务
  • 返回非零值表示相同,零表示不同
  • 用于比较pthread_t类型的线程ID

入参

名称 参数类型 详细说明 约束取值范围
t1 pthread_t 第一个线程ID 有效的线程ID
t2 pthread_t 第二个线程ID 有效的线程ID

返回值

返回值 文字含义 触发场景
非零 相同 两个线程ID相同
0 不同 两个线程ID不同

pthread_exit

void pthread_exit(void *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 终止调用线程
  • 通过retval参数向等待该线程的pthread_join调用返回线程退出值
  • 线程退出后,其资源根据分离状态决定回收方式

入参

名称 参数类型 详细说明 约束取值范围
retval void * 线程退出返回值 无限制

pthread_getschedparam

int pthread_getschedparam(pthread_t, int *__restrict, struct sched_param *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 获取指定线程的调度策略和调度参数
  • 目前仅支持SCHED_RR调度策略,不支持SCHED_OTHER、SCHED_FIFO
  • 通过出参返回调度策略和调度参数

入参

名称 参数类型 详细说明 约束取值范围
thread pthread_t 目标线程ID 有效的线程ID

出参

名称 数据类型 输出说明
policy int 调度策略
param struct sched_param 调度参数,包含优先级

返回值

返回值 文字含义 触发场景
0 成功 获取调度参数成功
Other 其他错误码 执行失败

pthread_getspecific

void *pthread_getspecific(pthread_key_t)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 获取调用线程中指定键的绑定值
  • 返回当前线程与指定键关联的数据指针
  • 若键未设置值则返回NULL

入参

名称 参数类型 详细说明 约束取值范围
key pthread_key_t 线程特定数据键 有效的key值

返回值

返回值 文字含义 触发场景
非NULL 成功 返回与键绑定的数据指针
NULL 未设置 键未设置值或键无效

pthread_join

int pthread_join(pthread_t, void **)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 阻塞等待指定线程结束
  • 可通过retval获取目标线程的退出值
  • 目标线程必须为PTHREAD_CREATE_JOINABLE状态

入参

名称 参数类型 详细说明 约束取值范围
thread pthread_t 目标线程ID 有效的可连接线程ID

出参

名称 数据类型 输出说明
retval void ** 线程退出返回值指针

返回值

返回值 文字含义 触发场景
0 成功 等待线程结束成功
Other 其他错误码 执行失败

pthread_key_create

int pthread_key_create(pthread_key_t *, void (*)(void *))

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 创建线程特定数据的键
  • 可选设置销毁回调函数,线程退出时调用
  • 创建的键可被进程中所有线程使用

入参

名称 参数类型 详细说明 约束取值范围
destructor void ()(void ) 销毁回调函数,线程退出时调用,参数为与键关联的线程特定数据值,回调返回值不被检查 NULL或有效的函数指针

出参

名称 数据类型 输出说明
key pthread_key_t 新创建的线程特定数据键

返回值

返回值 文字含义 触发场景
0 成功 键创建成功
Other 其他错误码 执行失败

pthread_key_delete

int pthread_key_delete(pthread_key_t)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 销毁线程特定数据键
  • 销毁后键不可再使用
  • 不会调用与键关联的销毁回调函数

入参

名称 参数类型 详细说明 约束取值范围
key pthread_key_t 待销毁的线程特定数据键 有效的key值

返回值

返回值 文字含义 触发场景
0 成功 键销毁成功
Other 其他错误码 执行失败

pthread_mutex_destroy

int pthread_mutex_destroy(pthread_mutex_t *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 销毁互斥锁
  • 释放互斥锁占用的资源
  • 销毁前需确保互斥锁未被锁定

入参

名称 参数类型 详细说明 约束取值范围
mutex pthread_mutex_t * 指向待销毁的互斥锁的指针 非NULL

返回值

返回值 文字含义 触发场景
0 成功 互斥锁销毁成功
Other 其他错误码 执行失败

pthread_mutex_getprioceiling

int pthread_mutex_getprioceiling(const pthread_mutex_t *__restrict, int *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 获取互斥锁的优先级上限
  • 通过出参返回优先级上限值
  • 优先级上限用于优先级继承协议

入参

名称 参数类型 详细说明 约束取值范围
mutex const pthread_mutex_t *__restrict 指向互斥锁的指针 非NULL,已初始化

出参

名称 数据类型 输出说明
prioceiling int 互斥锁的优先级上限

返回值

返回值 文字含义 触发场景
0 成功 获取优先级上限成功
Other 其他错误码 执行失败

pthread_mutex_init

int pthread_mutex_init(pthread_mutex_t *__restrict, const pthread_mutexattr_t *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 初始化互斥锁
  • 可通过属性对象设置互斥锁的类型、协议等属性
  • 若attr为NULL则使用默认属性

入参

名称 参数类型 详细说明 约束取值范围
mutex pthread_mutex_t *__restrict 指向待初始化的互斥锁的指针 非NULL
attr const pthread_mutexattr_t *__restrict 互斥锁属性对象指针 NULL或指向已初始化的属性对象

返回值

返回值 文字含义 触发场景
0 成功 互斥锁初始化成功
Other 其他错误码 执行失败

pthread_mutex_lock

int pthread_mutex_lock(pthread_mutex_t *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 申请互斥锁(阻塞操作)
  • 若互斥锁已被其他线程持有,调用线程将被阻塞
  • 获取互斥锁后继续执行

入参

名称 参数类型 详细说明 约束取值范围
mutex pthread_mutex_t * 指向互斥锁的指针 非NULL,已初始化

返回值

返回值 文字含义 触发场景
0 成功 互斥锁获取成功
Other 其他错误码 执行失败

pthread_mutex_setprioceiling

int pthread_mutex_setprioceiling(pthread_mutex_t *__restrict, int, int *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 设置互斥锁的优先级上限
  • 通过出参返回旧的优先级上限值
  • 优先级上限用于优先级保护协议

入参

名称 参数类型 详细说明 约束取值范围
mutex pthread_mutex_t *__restrict 指向互斥锁的指针 非NULL,已初始化
prioceiling int 新的优先级上限值 有效的优先级值

出参

名称 数据类型 输出说明
old_ceiling int 设置前的优先级上限

返回值

返回值 文字含义 触发场景
0 成功 设置优先级上限成功
Other 其他错误码 执行失败

pthread_mutex_timedlock

int pthread_mutex_timedlock(pthread_mutex_t *__restrict, const struct timespec *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 在指定时间内申请互斥锁
  • 若互斥锁在超时前可用则获取锁并返回
  • 超时后返回ETIMEDOUT

入参

名称 参数类型 详细说明 约束取值范围
mutex pthread_mutex_t *__restrict 指向互斥锁的指针 非NULL,已初始化
abstime const struct timespec *__restrict 超时时间 非NULL

返回值

返回值 文字含义 触发场景
0 成功 互斥锁获取成功
ETIMEDOUT 超时 等待超时
Other 其他错误码 执行失败

pthread_mutex_trylock

int pthread_mutex_trylock(pthread_mutex_t *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 尝试申请互斥锁(非阻塞)
  • 若互斥锁可用则获取锁并返回0
  • 若互斥锁已被其他线程持有则立即返回EBUSY

入参

名称 参数类型 详细说明 约束取值范围
mutex pthread_mutex_t * 指向互斥锁的指针 非NULL,已初始化

返回值

返回值 文字含义 触发场景
0 成功 互斥锁获取成功
EBUSY 互斥锁忙 互斥锁已被其他线程持有
Other 其他错误码 执行失败

pthread_mutex_unlock

int pthread_mutex_unlock(pthread_mutex_t *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 释放互斥锁
  • 释放后其他等待该互斥锁的线程可获取锁
  • 调用线程必须持有该互斥锁

入参

名称 参数类型 详细说明 约束取值范围
mutex pthread_mutex_t * 指向互斥锁的指针 非NULL,已初始化且已锁定

返回值

返回值 文字含义 触发场景
0 成功 互斥锁释放成功
Other 其他错误码 执行失败

pthread_mutexattr_destroy

int pthread_mutexattr_destroy(pthread_mutexattr_t *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 销毁互斥锁属性对象
  • 释放属性对象占用的资源
  • 属性对象销毁后不可再使用

入参

名称 参数类型 详细说明 约束取值范围
attr pthread_mutexattr_t * 指向待销毁的互斥锁属性对象的指针 非NULL

返回值

返回值 文字含义 触发场景
0 成功 属性对象销毁成功
Other 其他错误码 执行失败

pthread_mutexattr_getprioceiling

int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *__restrict, int *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 获取互斥锁属性中的优先级上限
  • 通过出参返回优先级上限值
  • 优先级上限用于优先级保护协议

入参

名称 参数类型 详细说明 约束取值范围
attr const pthread_mutexattr_t *__restrict 指向互斥锁属性对象的指针 非NULL

出参

名称 数据类型 输出说明
prioceiling int 优先级上限

返回值

返回值 文字含义 触发场景
0 成功 获取优先级上限成功
Other 其他错误码 执行失败

pthread_mutexattr_getprotocol

int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *__restrict, int *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 获取互斥锁属性中的协议属性
  • 通过出参返回协议属性值
  • 协议属性决定互斥锁的优先级继承行为

入参

名称 参数类型 详细说明 约束取值范围
attr const pthread_mutexattr_t *__restrict 指向互斥锁属性对象的指针 非NULL

出参

名称 数据类型 输出说明
protocol int 协议属性值

返回值

返回值 文字含义 触发场景
0 成功 获取协议属性成功
Other 其他错误码 执行失败

pthread_mutexattr_gettype

int pthread_mutexattr_gettype(const pthread_mutexattr_t *__restrict, int *__restrict)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 获取互斥锁属性中的类型属性
  • 通过出参返回互斥锁类型值
  • 类型决定互斥锁的锁定行为(如是否支持递归锁)

入参

名称 参数类型 详细说明 约束取值范围
attr const pthread_mutexattr_t *__restrict 指向互斥锁属性对象的指针 非NULL

出参

名称 数据类型 输出说明
type int 互斥锁类型值

返回值

返回值 文字含义 触发场景
0 成功 获取类型属性成功
Other 其他错误码 执行失败

pthread_mutexattr_init

int pthread_mutexattr_init(pthread_mutexattr_t *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 初始化互斥锁属性对象
  • 将属性对象设置为默认值
  • 初始化后的属性对象可用于pthread_mutex_init

入参

名称 参数类型 详细说明 约束取值范围
attr pthread_mutexattr_t * 指向待初始化的互斥锁属性对象的指针 非NULL

返回值

返回值 文字含义 触发场景
0 成功 属性对象初始化成功
Other 其他错误码 执行失败

pthread_mutexattr_setprioceiling

int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, int)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 设置互斥锁属性中的优先级上限
  • 优先级上限用于优先级保护协议
  • 设置后新建的互斥锁将使用该优先级上限

入参

名称 参数类型 详细说明 约束取值范围
attr pthread_mutexattr_t * 指向互斥锁属性对象的指针 非NULL
prioceiling int 优先级上限值 有效的优先级值

返回值

返回值 文字含义 触发场景
0 成功 设置优先级上限成功
Other 其他错误码 执行失败

pthread_mutexattr_setprotocol

int pthread_mutexattr_setprotocol(pthread_mutexattr_t *, int)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 设置互斥锁属性中的协议属性
  • 协议属性决定互斥锁的优先级继承行为
  • 设置后新建的互斥锁将使用该协议

入参

名称 参数类型 详细说明 约束取值范围
attr pthread_mutexattr_t * 指向互斥锁属性对象的指针 非NULL
protocol int 协议属性值 - PTHREAD_PRIO_NONE
- PTHREAD_PRIO_INHERIT
- PTHREAD_PRIO_PROTECT

返回值

返回值 文字含义 触发场景
0 成功 设置协议属性成功
Other 其他错误码 执行失败

pthread_mutexattr_settype

int pthread_mutexattr_settype(pthread_mutexattr_t *, int)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 设置互斥锁属性中的类型属性
  • 类型决定互斥锁的锁定行为
  • 设置后新建的互斥锁将使用该类型

入参

名称 参数类型 详细说明 约束取值范围
attr pthread_mutexattr_t * 指向互斥锁属性对象的指针 非NULL
type int 互斥锁类型值 - PTHREAD_MUTEX_NORMAL
- PTHREAD_MUTEX_RECURSIVE
- PTHREAD_MUTEX_ERRORCHECK

返回值

返回值 文字含义 触发场景
0 成功 设置类型属性成功
Other 其他错误码 执行失败

pthread_once

int pthread_once(pthread_once_t *, void (*)(void))

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 执行一次性初始化操作
  • 无论多少线程调用pthread_once,init_routine仅执行一次
  • once_control用于判断初始化是否已完成

入参

名称 参数类型 详细说明 约束取值范围
once_control pthread_once_t * 一次性初始化控制变量 非NULL,需初始化为PTHREAD_ONCE_INIT
init_routine void (*)(void) 初始化函数指针 非NULL

返回值

返回值 文字含义 触发场景
0 成功 一次性操作完成
Other 其他错误码 执行失败

pthread_self

pthread_t pthread_self(void)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 获取调用线程自身的线程ID
  • 返回当前线程的pthread_t标识
  • 可用于线程自识别和线程管理

返回值

返回值 文字含义 触发场景
pthread_t 当前线程ID 调用成功

pthread_setcancelstate

int pthread_setcancelstate(int, int *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 设置调用线程的取消状态
  • 取消状态决定线程是否接受取消请求
  • 通过出参返回之前的取消状态

入参

名称 参数类型 详细说明 约束取值范围
state int 新的取消状态 - PTHREAD_CANCEL_ENABLE
- PTHREAD_CANCEL_DISABLE

出参

名称 数据类型 输出说明
oldstate int 设置前的取消状态

返回值

返回值 文字含义 触发场景
0 成功 设置取消状态成功
Other 其他错误码 执行失败

pthread_setcanceltype

int pthread_setcanceltype(int, int *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 设置调用线程的取消类型
  • 取消类型决定取消请求的响应方式
  • 通过出参返回之前的取消类型

入参

名称 参数类型 详细说明 约束取值范围
type int 新的取消类型 - PTHREAD_CANCEL_DEFERRED
- PTHREAD_CANCEL_ASYNCHRONOUS

出参

名称 数据类型 输出说明
oldtype int 设置前的取消类型

返回值

返回值 文字含义 触发场景
0 成功 设置取消类型成功
Other 其他错误码 执行失败

pthread_setschedparam

int pthread_setschedparam(pthread_t, int, const struct sched_param *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 设置指定线程的调度策略和调度参数
  • 目前仅支持SCHED_RR调度策略,不支持SCHED_OTHER和SCHED_FIFO
  • 任务优先级仅支持0~31

入参

名称 参数类型 详细说明 约束取值范围
thread pthread_t 目标线程ID 有效的线程ID
policy int 调度策略 SCHED_RR
param const struct sched_param * 调度参数 非NULL,sched_priority范围0~31

返回值

返回值 文字含义 触发场景
0 成功 设置调度参数成功
Other 其他错误码 执行失败

pthread_setschedprio

int pthread_setschedprio(pthread_t, int)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 设置指定线程的调度优先级
  • 任务优先级仅支持0~31
  • 不改变线程的调度策略

入参

名称 参数类型 详细说明 约束取值范围
thread pthread_t 目标线程ID 有效的线程ID
prio int 新的优先级值 0~31

返回值

返回值 文字含义 触发场景
0 成功 设置优先级成功
Other 其他错误码 执行失败

pthread_setspecific

int pthread_setspecific(pthread_key_t, const void *)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 设置调用线程中指定键的绑定值
  • 将value与key关联,仅当前线程可见
  • 不同线程可对同一键设置不同的值

入参

名称 参数类型 详细说明 约束取值范围
key pthread_key_t 线程特定数据键 有效的key值
value const void * 与键绑定的数据指针 无限制

返回值

返回值 文字含义 触发场景
0 成功 设置线程数据成功
Other 其他错误码 执行失败

pthread_testcancel

void pthread_testcancel(void)

头文件清单

#include "open_source/musl/include/pthread.h"

功能说明

  • 检查是否有挂起的取消请求
  • 若存在挂起的取消请求且取消状态为启用,则线程终止
  • 用于在长时间运行的无取消点代码中主动创建取消点

Structures

pthread_attr_t

typedef struct __pthread_attr_s {
    unsigned int detachstate;
    unsigned int schedpolicy;
    struct sched_param schedparam;
    unsigned int inheritsched;
    unsigned int scope;
    unsigned int stackaddr_set;
    void* stackaddr;
    unsigned int stacksize_set;
    size_t stacksize;
#ifdef LOSCFG_KERNEL_SMP
    cpu_set_t cpuset;
#endif
} pthread_attr_t;

成员说明

成员名称 数据类型 描述
detachstate unsigned int 分离状态
schedpolicy unsigned int 调度策略
schedparam struct sched_param 调度参数
inheritsched unsigned int 调度继承方式
scope unsigned int 竞争范围
stackaddr_set unsigned int 堆栈地址是否已设置
stackaddr void* 堆栈起始地址
stacksize_set unsigned int 堆栈大小是否已设置
stacksize size_t 堆栈大小
cpuset cpu_set_t CPU亲和性掩码(仅在LOSCFG_KERNEL_SMP配置下可用)

Macros

PTHREAD_CREATE_JOINABLE

#define PTHREAD_CREATE_JOINABLE 0

PTHREAD_CREATE_DETACHED

#define PTHREAD_CREATE_DETACHED 1

PTHREAD_MUTEX_NORMAL

#define PTHREAD_MUTEX_NORMAL 0

PTHREAD_MUTEX_RECURSIVE

#define PTHREAD_MUTEX_RECURSIVE 1

PTHREAD_MUTEX_ERRORCHECK

#define PTHREAD_MUTEX_ERRORCHECK 2

PTHREAD_PRIO_NONE

#define PTHREAD_PRIO_NONE 0

PTHREAD_PRIO_INHERIT

#define PTHREAD_PRIO_INHERIT 1

PTHREAD_PRIO_PROTECT

#define PTHREAD_PRIO_PROTECT 2

PTHREAD_INHERIT_SCHED

#define PTHREAD_INHERIT_SCHED 0

PTHREAD_EXPLICIT_SCHED

#define PTHREAD_EXPLICIT_SCHED 1

PTHREAD_SCOPE_SYSTEM

#define PTHREAD_SCOPE_SYSTEM 0

PTHREAD_SCOPE_PROCESS

#define PTHREAD_SCOPE_PROCESS 1

PTHREAD_PROCESS_PRIVATE

#define PTHREAD_PROCESS_PRIVATE 0

PTHREAD_PROCESS_SHARED

#define PTHREAD_PROCESS_SHARED 1

PTHREAD_ONCE_INIT

#define PTHREAD_ONCE_INIT 0

PTHREAD_CANCEL_ENABLE

#define PTHREAD_CANCEL_ENABLE 0

PTHREAD_CANCEL_DISABLE

#define PTHREAD_CANCEL_DISABLE 1

PTHREAD_CANCEL_DEFERRED

#define PTHREAD_CANCEL_DEFERRED 0

PTHREAD_CANCEL_ASYNCHRONOUS

#define PTHREAD_CANCEL_ASYNCHRONOUS 1