How to print pthread_t

Viewed 116660

Searched, but don't come across a satisfying answer.

I know there's no a portable way to print a pthread_t.

How do you do it in your app?

Update:

Actually I don't need pthread_t, but some small numeric id, identifying in debug message different threads.

On my system (64 bit RHEL 5.3) it's defined as unsigned long int, so it's big number and just printing it eats a valuable place in debug line. How does gdb assign short tids?

13 Answers

No need to get fancy, you can treat pthread_t as a pointer without a cast:

printf("awesome worker thread id %p\n", awesome_worker_thread_id);

Output:

awesome worker thread id 0x6000485e0

As James mentioned above, the best way is to look at the header where the type is defined.

You can find the definition of pthread_t in pthreadtypes.h which can be found at:

/usr/include/bits/pthreadtypes.h

Related