为什么打印指向 pthread 类型结构的指针,给我们线程 ID?

Why does printing the pointer to structure of type pthread, gives us thread ID?

pthread的结构如下。摘自https://stuff.mit.edu/afs/sipb/project/pthreads/include/pthread.h

    struct pthread {
    struct machdep_pthread  machdep_data;
    enum pthread_state      state;
    pthread_attr_t          attr;

    /* Signal interface */
    sigset_t                sigmask;
    sigset_t                sigpending;

    /* Time until timeout */
    struct timespec         wakeup_time;

    /* Cleanup handlers Link List */
    struct pthread_cleanup  *cleanup;

    /* Join queue for waiting threads */
    struct pthread_queue    join_queue;

    /* Queue thread is waiting on, (mutexes, cond. etc.) */
    struct pthread_queue    *queue;

    /*
     * Thread implementations are just multiple queue type implemenations,
     * Below are the various link lists currently necessary
     * It is possible for a thread to be on multiple, or even all the
     * queues at once, much care must be taken during queue manipulation.
     *
     * The pthread structure must be locked before you can even look at
     * the link lists.
     */ 

    struct pthread          *pll;       /* ALL threads, in any state */
    /* struct pthread       *rll;        Current run queue, before resced */
    struct pthread          *sll;       /* For sleeping threads */
    struct pthread          *next;      /* Standard for mutexes, etc ... */
    /* struct pthread           *fd_next;    For kernel fd operations */

    int                     fd;         /* Used when thread waiting on fd */

    semaphore               lock;

    /* Data that doesn't need to be locked */
    void                    *ret;
    int                     error;
    const void              **specific_data;
};

    typedef struct pthread *        pthread_t;

现在让我们看一下打印线程ID的代码:

#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h> 
void* calls(void* ptr) 
{ 
    // using pthread_self() get current thread id 
    printf("In function \nthread id = %ld\n", pthread_self()); 
    pthread_exit(NULL); 
    return NULL; 
} 

int main() 
{ 
    pthread_t thread; // declare thread 
    pthread_create(&thread, NULL, calls, NULL); 
    printf("In main \nthread id = %ld\n", thread);  
    pthread_join(thread, NULL);  
    return 0; 
} 

我系统的输出是:

In main 
thread id = 140289852200704
In function 
thread id = 140289852200704

来自pthread.h文件(上面),pthread是一个结构,代码中的thread是指向结构pthread的指针(因为pthread_ttypdef struct pthread*) .为什么打印此指针会为我们提供线程 ID?

From pthread.h file (above), pthread is a structure, thread in the code is a pointer to the structure pthread (since pthread_t is typdef struct pthread*).

明确一点:在该实现中pthread_t 是指向结构的指针类型。我想这对于 pthreads 实现来说很常见,但要小心避免将特定实现的细节误认为是规范或所有实现的一般特征。例如,它也可以是其他一些实现中的整数索引,以及其他各种可能性。

Why does printing this pointer gives us the thread ID?

因为它线程ID。因为你很幸运,使用 %d 格式化指令打印它所产生的未定义行为在两个地方都以相同的方式表现出来。

您可能在幕后查看实施的定义 pthread_t 给自己造成了伤害。您不需要知道这些细节来使用 pthreads,事实上它们对您没有丝毫帮助。该类型应被视为不透明。

要回答这个问题,你真正需要了解的是 pthread_create() 写入变量 thread 的值是创建的线程的 ID,而 pthread_self() 返回的值是调用线程的线程 ID。当然,每种获取线程 ID 的机制都会为同一线程产生相同的 ID。