how to get thread id of a pthread in linux c program?

后端 未结 11 689
忘了有多久
忘了有多久 2020-12-02 07:05

In linux c program, how to print thread id of a thread created by pthread library?
for ex: we can get pid of a process by getpid()

相关标签:
11条回答
  • 2020-12-02 07:35

    As noted in other answers, pthreads does not define a platform-independent way to retrieve an integral thread ID.

    On Linux systems, you can get thread ID thus:

    #include <sys/types.h>
    pid_t tid = gettid();
    

    On many BSD-based platforms, this answer https://stackoverflow.com/a/21206357/316487 gives a non-portable way.

    However, if the reason you think you need a thread ID is to know whether you're running on the same or different thread to another thread you control, you might find some utility 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're on the main thread, there are additional ways, documented in answers to this question how can I tell if pthread_self is the main (first) thread in the process?.

    0 讨论(0)
  • 2020-12-02 07:35

    You can use pthread_self()

    The parent gets to know the thread id after the pthread_create() is executed sucessfully, but while executing the thread if we want to access the thread id we have to use the function pthread_self().

    0 讨论(0)
  • 2020-12-02 07:36

    Platform-independent way (starting from c++11) is:

    #include <thread>
    
    std::this_thread::get_id();
    
    0 讨论(0)
  • 2020-12-02 07:37

    pthread_getthreadid_np wasn't on my Mac os x. pthread_t is an opaque type. Don't beat your head over it. Just assign it to void* and call it good. If you need to printf use %p.

    0 讨论(0)
  • 2020-12-02 07:41
    pid_t tid = syscall(SYS_gettid);
    

    Linux provides such system call to allow you get id of a thread.

    0 讨论(0)
  • 2020-12-02 07:45

    I think not only is the question not clear but most people also are not cognizant of the difference. Examine the following saying,

    POSIX thread IDs are not the same as the thread IDs returned by the Linux specific gettid() system call. POSIX thread IDs are assigned and maintained by the threading implementation. The thread ID returned by gettid() is a number (similar to a process ID) that is assigned by the kernel. Although each POSIX thread has a unique kernel thread ID in the Linux NPTL threading implementation, an application generally doesn’t need to know about the kernel IDs (and won’t be portable if it depends on knowing them).

    Excerpted from: The Linux Programming Interface: A Linux and UNIX System Programming Handbook, Michael Kerrisk

    IMHO, there is only one portable way that pass a structure in which define a variable holding numbers in an ascending manner e.g. 1,2,3... to per thread. By doing this, threads' id can be kept track. Nonetheless, int pthread_equal(tid1, tid2) function should be used.

    if (pthread_equal(tid1, tid2)) printf("Thread 2 is same as thread 1.\n");
    else printf("Thread 2 is NOT same as thread 1.\n");
    
    0 讨论(0)
提交回复
热议问题