Suppose we have many user processes running on Linux. Each process has many threads running.
I can get process ID by calling getpid()
, the return value of which is an integer.
I can get thread ID by calling pthread_self()
, the return value of which is an opaque type called pthread_t
.
Now I need to store the process ID (an int, typically 4 bytes) and the thread ID (pthread_t
, need to figure out how many bytes) in shared memory so that I can later use the two pieces of ID information to identify that specific thread and to check if the thread is still running or not.
I've found many online sources cast pthread_t
to either unsigned int
or unsigned long
. Since I don't want any data loss during the casting, how should I deal with the pthread_t
data so that it's a fixed-size piece of data (as mentioned, I need to store the thread information in shared memory).
Also, how should I identify that specific thread by combination of process ID and thread ID later? How to check if the thread is still running or not?