glibc: pthread: pthread_self 和 getpid 不是一个数

上面这个结论在使用说明里有明确的说明;
pthread_self - obtain ID of the calling thread
#include <pthread.h>
pthread_t pthread_self(void);
Compile and link with -pthread.

The pthread_self() function returns the ID of the calling thread. 这个值和pthread_create返回的值是一个值。
返回值就是pthread_t值,永远不会失败。
但是POSIX.1,允许实现的自由,不管是返回一个数值,还是一个结构体,都是允许的,只要可以唯一表示一个线程。所以pthread_t类型变量不能使用两个等号来进行比较,而应该使用pthread_equal(3)。

Thread identifiers should be considered opaque: any attempt to use a thread ID other than in pthreads calls is nonportable and can lead to unspecified results.

Thread IDs are guaranteed to be unique only within a process. A thread ID may be reused after a terminated thread has been joined, or a detached thread has terminated.

The thread ID returned by pthread_self() is not the same thing as the kernel thread ID returned by a call to gettid(2).

从glibc的实现,就x86来说是,使用了寄存器fs&#x

猜你喜欢

转载自blog.csdn.net/qq_36428903/article/details/131835035