线程的应用

1)pthread_create()函数:创建线程

pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg)

2)pthread_self()函数:获取线程的ID

pthread_self(void)

3)pthread_equal()函数:比较两个线程ID是否相等

pthread_equal(pthread_t t1, pthread_t t2)

4)pthread_join()函数:等待线程退出

pthread_join(pthread_t thread, void **retval)

5)pthread_detach()函数:从状态上实现线程分离,线程一旦终止就立刻回收它占用的所有资源

pthread_detach(pthread_t thread)

1.分别编译线程在正常进行与分离状态下的C文件:

2.结果分别为:

3.由以上结果可知,使用pthread_detach(pth)函数可将线程pth分离,使其在结束时能够自动释放资源。

由于使用了pthread_detach将线程设置为分离状态,则主线程无法再通过pthread_join获取到该线程的退出状态,因此输出错误信息。

6)pthread_canel()函数:线程的取消

pthread_cancel(pthread_t thread)

7)pthread_cleanup_push ()/ pthread_cleanup_pop()函数:线程退出,清理函数注册和执行

pthread_cleanup_push(void (*rtn)(void *),void *arg)

pthread_cleanup_pop(int execute)

8)pthread_exit()函数:退出线程

pthread_exit(void  *retval)

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值