线程原语

线程原语

比较两个线程是否相等
#include<pthread.h>
int pthread_equal(pthread_t tid1,pthread_t tid2);
相等--非0,否则-- 0


获得自身的线程ID
#include<pthread.h>
pthread_t  pthread_self(void);


创建线程
#include<pthread.h>
int pthread_create(pthread_t *tid,const pthread_attr_t *attr,void *(*thr_func)(void *),void *arg);
成功-- 0,否则-- 返回错误编号
参数:
tid -- 线程ID会被存在tid指向的内存空间
attr -- 定制各种线程属性,NULL是默认线程属性
thr_fun -- 线程执行的函数,当函数有多个要传入的参数是,arg就设置为结构体


线程退出
#include<pthread.h>
void pthread_exit(void *rval_ptr);
参数:
rval_ptr -- 退出参数,其他线程可以通过pthread_join函数


将线程置于分离状态,让系统回收资源
#include<pthread.h>
int pthread_join(pthread_t thread, void **rval_ptr);
成功-- 0,出错-- 返回错误编号
参数:
rval_ptr -- 若是线程从启动返回或调用pthread_exit函数,rvalptr就包含返回码,
若是线程被取消,rvalptr指向的内存单元就设置为PTHREAD_CANCELED
如果线程已经是分离态,调用此函数就会出错,返回EINVAL
若对线程的返回值不感兴趣可以设置为NULL。

取消同一进程的其他线程
#include<pthread.h>
int pthread_cancel(pthread_t tid);
成功-- 0,出错--返回错误编号



线程清理处理函数
#include<pthread.h>
void pthread_cleanup_push(void (*clean)(void *), void *arg);
void pthread_cleanup_pop(int execute);

参数:
execute -- 若为0,则上一个清理函数不执行。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值