线程控制原语

线程控制原语:对线程控制的函数

连接编译时需要加参数: -lpthread

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。

pthread_create函数

创建一个新线程。                 其作用,对应进程中fork() 函数。

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

         返回值:成功:0;      失败:返回错误号        -----Linux环境下,所有线程特点,失败均直接返回错误号。

猜你喜欢

转载自blog.csdn.net/weixin_42067873/article/details/102765291
今日推荐