pthread_self函数用法详解
pthread_self函数简介
- 头文件包含
#include <pthread.h>
- 函数定义
pthread_t pthread_self(void);
- 编译链接选项
-pthread
pthread_self函数常见使用错误
- 链接错误
undefined reference to `pthread_self'
解决办法:添加链接选项
-pthread
- 编译错误
warning: implicit declaration of function ‘pthread_self’ [-Wimplicit-function-declaration]
解决办法:包含头文件
#include <pthread.h>
pthread_self函数详细描述
pthread_self ()函数返回调用线程的ID。这与创建该线程的pthread_create (3)调用中的*thread返回的值相同。
pthread_self函数返回值
这个函数总是成功,返回调用线程的ID。
pthread_self函数错误码
这个函数总是成功的。
pthread_self函数其他说明
POSIX.1允许实现在选择用于表示线程ID的类型方面有很大的自由度;例如,允许使用算术类型或结构表示。因此,pthread_t类型的变量不能使用C等式运算符(PPP2)进行可移植性比较;改用pthread_equal (3)。
线程标识符应该被认为是不透明的:任何在pthreads调用之外使用线程ID的尝试都是不可移植的,并可能导致未指定的结果。
线程ID只在进程内保证是唯一的。线程ID可以在连接终止的线程或终止分离的线程后重用。
pthread_self ()返回的线程ID与调用gettid (2)返回的内核线程ID不是一回事
评论区