As noted in other answers, pthreads does not define a platform-independent way of obtaining an integral thread identifier.
On Linux systems, you can get the thread id like this:
#include <sys/types.h> pid_t tid = gettid();
On many BSD-based platforms, this answer https://stackoverflow.com/a/212251/ ... gives a non-portable way.
However, if the reason you need a thread identifier is to know if you are working on the same thread with another thread that you control, you may find some usefulness in this approach
static pthread_t threadA; // On thread A... threadA = pthread_self(); // On thread B... pthread_t threadB = pthread_self(); if (pthread_equal(threadA, threadB)) printf("Thread B is same as thread A.\n"); else printf("Thread B is NOT same as thread A.\n");
If you just need to know if you are in the main thread, there are additional ways documented in the answers to this question, how can I determine if pthread_self is the main (first) in the process? .
bleater Dec 01 2018-12-14T00: 00Z
source share