pthread.h常用函数

pthread.h

默认不包含在库中,gcc编译时需要链接-lpthread

pthread_create()

#include <pthread.h>
 
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                   void *(*start_routine) (void *), void *arg);

功能:
	创建一个子线程
参数:
	thread : 线程ID变量指针 (类型实质是 typedef unsigned long int)
    attr : 线程属性,默认属性可设置为NULL
    start_routinge : 线程执行函数
    arg : 线程执行函数的参数, 无需传参可填NULL
返回值:
	成功 : 0
    失败 : 错误码

一旦子线程创建成功,则会被独立调度执行,并且与其他线程并发执行

pthread_exit()

#include <pthread.h>

void pthread_exit(void *retval);

功能:
	让线程退出,并返回值
参数:
	retval : 线程返回值,通过指针传递
  1. 当主线程调用 pthread_exit() 函数时,进程不会结束,也不会导致其他子线程退出

  2. 任何线程调用 exit() 函数会让进程结束

pthread_join()

#include <pthread.h>

int pthread_join(pthread_t thread,void **retval);

功能:
	等待子线程退出,并释放子线程资源
参数:
	thread : 线程ID
    retval : 获取线程退出值的指针
返回值:
    成功 : 0
	失败 : 错误码

pthread_detach()

#include <pthread.h>

int pthread_detach(pthread_t thread);

功能:
	将子线程与主线程分离,分离后的子线程不再适用 pthread_join(),
    分离后的子线程退出后,会由操作系统自动释放该线程的资源
参数:
	thread : 线程ID
返回值:
	成功 : 0
   	失败 : 错误码

线程分离函数不会阻塞线程的执行

pthread_mutex_init()

#include<pthread.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;//静态初始化互斥锁
int pthread_mutex_init(pthread_mutex_t *restrict mutex,
                       const pthread_mutexattr_t *restrict attr); //动态初始化互斥锁

功能:
	初始化线程互斥锁
参数:
    mutex : 线程互斥锁对象指针
    attr : 线程互斥锁属性,默认可填NULL
返回值:
    成功 : 0
    失败 : 错误码

pthread_mutex_destroy()

#include <pthread.h>

int pthread_mutex_destroy(pthread_mutex_t *mutex);

功能:
	销毁线程互斥锁
参数:
	mutex : 线程互斥锁指针
返回值:
    成功 : 0
    失败 : 错误编码

pthread_mutex_lock()

#include <pthread.h>

int pthread_mutex_lock(pthread_mutex_t *mutex);

功能:
	抢占线程互斥锁,未抢到则阻塞直到抢占成功
参数:
	mutex : 线程互斥锁指针
返回值:
	成功 : 0
    失败 : 错误码

pthread_mutex_unlock()

#include <pthread.h>

int pthread_mutex_unlock(pthread_mutex_t *mutex);

功能:
	释放线程互斥锁
参数:
	mutex : 线程互斥锁指针
返回值:
	成功 : 0
    失败 : 错误码

pthread_self()

#include <pthread.h>

pthread_t pthread_self(void);

功能:
	获取当前线程线程ID
返回值:
	当前线程线程ID

pthread_cond_init()

#include <pthread.h>

pthread_cond_t cond = PTHREAD_COND_INITIALIZER; //静态初始化条件变量
int pthread_cond_init(pthread_cond_t *restrict cond,
                      const pthread_condattr_t *restrict attr); //动态初始化条件变量

功能:
	初始化条件变量
参数:
    cond : 条件变量指针
    attr : 条件变量属性,默认可填NULL
返回值:
    成功 : 0
    失败 : 错误码

pthread_cond_destroy()

#include <pthread.h>

int pthread_cond_destroy(pthread_cond_t *cond);

功能:
	销毁条件变量
参数:
	cond :条件变量指针
返回值:
    成功 : 返回 0
    失败 : 返回错误码

pthread_cond_wait()

#include <pthread.h>

int pthread_cond_wait(pthread_cond_t *restrict cond,
                      pthread_mutex_t *restrict mutex);

功能:
    释放锁并阻塞线程,等待唤醒重新竞争锁,竞争成功函数返回,失败继续阻塞
参数:
    cond : 条件变量指针
    mutex : 关联互斥锁指针
返回值:
    成功 : 返回 0
    失败 : 返回 错误码
  • 条件变量需要与互斥锁结合使用,先获得锁才能进行条件变量的操作
  • 调用函数后会释放锁,并阻塞线程
  • 一旦线程唤醒,需要重新竞争锁,重新获得锁之后,pthread_cond_wait 函数返回

pthread_cond_signal()

#include <pthread.h>

int pthread_cond_signal(pthread_cond_t *cond);

功能:
	唤醒阻塞在指定条件的至少一个线程
参数:
	cond : 条件变量指针
返回值:
	成功 : 0
    失败 :错误码
  • 通常情况下该函数只唤醒一个线程,但由于cpu架构或操作系统调度,可能会导致虚假唤醒的情况,从而唤醒多于一个线程。

  • 使用while代替if作为ptheard_cond_wait()的判断条件,可以有效避免虚假唤醒带来的意外影响

pthread_cond_broadcast()

#include <pthread.h>

int pthread_cond_broadcast(pthread_cond_t *cond);

功能:
	唤醒阻塞在指定条件的所有线程
参数:
	cond : 条件变量指针
返回值:
	成功 : 0
    失败 :错误码
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值