Skip to content
Advertisement

pthread_self() doesn’t return a meaningful thread id?

#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<sys/types.h>
int main(){
    int systid=syscall(186);
    int pt_tid=pthread_self();
    pid_t id=getpid();
    printf("pid=%d,tid=%d,pt_tid=%dn",id,systid,pt_tid);
    return 0;
}

I was running this program on RHEL 5 with gcc4.1.2.

$gcc testtid.c -lpthread && ./a.out
pid=35086,tid=35086,pt_tid=1295541984

Seems the syscall can give the correct thread id(same like process id), but pthread_self doesn’t give meaningful result.

Is it because pthread_self is not portable?

Advertisement

Answer

If you read man pthread_self, which you should:

Thread identifiers should be considered opaque: any attempt to use a thread ID other than in pthreads calls is non-portable 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).

Advertisement