欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

linux c之使用pthread_create创建线程pthread_join等待线程和pthread_exit终止线程总结

发布时间:2023/12/4 80 豆豆
生活随笔 收集整理的这篇文章主要介绍了 linux c之使用pthread_create创建线程pthread_join等待线程和pthread_exit终止线程总结 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

1、介绍API

1、pthread_create函数


函数简介
pthread_create是UNIX环境创建线程函数
头文件

#include<pthread.h>


函数声明

 

int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void *restrict arg);

 


返回值
若成功则返回0,否则返回出错编号
参数
第一个参数为指向线程标识符的指针,第二个参数用来设置线程属性,第三个参数是线程运行函数的起始地址, 最后一个参数是运行函数的参数。
另外在编译时注意加上-lpthread参数,以调用静态链接库。因为pthread并非Linux系统的默认库

 

 

2、pthread_join函数


函数简介
函数pthread_join用来等待一个线程的结束。
函数原型为:

extern int pthread_join __P (pthread_t __th, void **__thread_return);

 



参数:
第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值

总结

以上是生活随笔为你收集整理的linux c之使用pthread_create创建线程pthread_join等待线程和pthread_exit终止线程总结的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。