linux环境编程-线程的相关函数

前面我详细的介绍了 线程和进程的区别【在linux下】,和线程的概念。现在我们来介绍一下线程的控制语句(人话就是操作线程的函数)

1.pthread_ self 函数获取线程ID。其作用对应进程中getpid() 函数。
pthread_ t pthread self(void);
返回值:成功: 0;
失败:无!
线程ID: pthread t类型,本质:在Linux下为无符号整数(%lu),其他系统中可能是结构体实现
线程ID是进程内部,识别标志。(两个进程间,线程ID允许相同)
注意:不应使用全局变量pthread _t tid,在子线程中通过pthread_ create 传出参数来获取线程ID,而应使用pthread_ self。
 

2.pthread_ create 函数创建一个新线程。
其作用,对应进程中fork)函数。
int pthread create(pthread _t *thread, const pthread attr _t *attr, void *(*start_ routine) (void *), void * arg);
返回值:成功: 0;
失败:错误号---inux 环境下,所有线程特点,失败均直接返回错误号。
参数:
pthread t:当前Linux中可理解为: typedef unsigned long int pthread _t;
参数1:传出参数,保存系统为我们分配好的线程ID
参数2:通常传NULL,表示使用线程默认属性。若想使用具体属性也可以修改该参数。
参数3:函数指针࿰

猜你喜欢

转载自blog.csdn.net/qq_44065088/article/details/109155127
今日推荐