Home > Net >  Do threads (pthreads) automatically get assigned ids in posix?
Do threads (pthreads) automatically get assigned ids in posix?

Time:12-08

I'm not understanding the difference between creating a thread (say: pthread_t t1;) & thread id. When I tried printing the thread id using pthread_self() it gave a random number. Are threads automatically assigned an id then? How can we give a pthread an id?

The code here is for context:

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void * PrintHello(){
     printf("Thread%d: Hello World!\n", pthread_self()); 
}

int main(){
     pthread_t t1, t2;
     pthread_create(&t1, NULL, &PrintHello, NULL);
     pthread_create(&t2, NULL, &PrintHello, NULL);

     pthread_join(t1, NULL);
     pthread_join(t2, NULL);
}

CodePudding user response:

pthread_create creates a new thread and . A thread is a kernel resource and it is controlled using a descriptor, that is, a data structure (typically an integer) from the user point of view.

It is a bit like when you request a command on a physical/online shop and someone give you a command ID once the command is paid. This command ID is useful to probe the state of the command and request for modifications. The commend ID is not the command itself : as a user, you can only get an ID. The data structure containing the information about the commend is stored on an information system (eg. database) of the physical/online shop. For pthreads, the thread ID is like a command ID and the shop is the kernel.

When I tried printing the thread id using pthread_self() it gave a random number.

A pthread_t data structure is an opaque data type. You should not print it. If you do then the result will be undefined (platform-dependent). Here is what the documentation states:

POSIX.1 allows an implementation wide freedom in choosing the type used to represent a thread ID; for example, representation using either an arithmetic type or a structure is permitted. Therefore, variables of type pthread_t can't portably be compared using the C equality operator (==); use pthread_equal(3) instead.
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).

If you want to print a thread ID like the one printed in top, htop, etc. then you should use the function gettid which returns a pid_t type which is an integer. Note gettid is a thread ID in the context of the Linux scheduler while pthread_self also return a thread ID, but a pthread_t in the context of pthread only meant to control/probe the state of a thread or compare its ID to others (using pthread_equal). A pthread_t type is useful to interact with threads using the pthread API : for example to wait a thread (pthread_join). A pid_t is useful to interact with the Linux kernel scheduler : for example to change the priority of a thread or its affinity to cores. More details are available in this post or this one.

Are threads automatically assigned an id then?

Yes. When a thread is created, it has an associated pthread_t value and a pid_t ones.

How can we give a pthread an id?

AFAIK, a user cannot set them. This is the job of pthread (for pthread_t) and the kernel (for pid_t).

  • Related