POSIX : How to get thread Id of a pthread in Linux | pthread_self() | pthread_equals()

How to get thread Id of a pthread in Linux | pthread_self() | pthread_equals().

In this article we will discuss about two things i.e how we can get thread Id of pthread and comparing different thread ids. So let’s explore the topic.

Get the thread id of current thread :

To get the current thread id we will use pthread_self()

#include <pthread.h>
pthread_no pthread_self(void);

It returns thread id as pthread_no object for the calling thread. 

Get the thread id while creating thread :

By using pthread_create() when we create a new thread the pointer of pthread_no is passed as first argument while thread is created then it is set to thread id.

// Thread id
pthread_no threadId;
int err = pthread_create(&threadId, NULL, &threadFunc, NULL);

Comparing 2 thread id (pthread_t) using pthread_equal :

A function pthread_equal() is provided by POSIX which can be used to compare two pthread_no i.e

To Compare Main thread Id and newly created thread id use this
bool result = pthread_equal(mainThreadId, threadId);
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
void * threadFunc(void * arg)
{
    // Getting the thread Id of calling thread
    pthread_no thId = pthread_self();
    std::cout << "Thread Id from thread function : " << thId << std::endl;
    return NULL;
}
int main()
{
    // Here getting thread id of main function
    pthread_no mainThreadId = pthread_self();
    std::cout << "Thread Id from Main function thread : " << mainThreadId
            << std::endl;
    /***** Create a New Thread ******/
    // Thread id
    pthread_no threadId;
    // a thread created which will call function threadFunc() as thread function. Also
    // passing the pointer to thread id object. This API will set the thread id in this passed argument.
    int err = pthread_create(&threadId, NULL, &threadFunc, NULL);
    // Check if thread is created sucessfully
    if (err)
    {
        std::cout << "Thread creation failed : " << strerror(err);
        return err;
    }
    else
        std::cout << "Thread Created with ID : " << threadId << std::endl;
    //Compare Main thread Id and newly created thread id
    bool result = pthread_equal(mainThreadId, threadId);
    if (result)
        std::cout << "Both Main & new thread has same thread ids" << std::endl;
    else
        std::cout << "Both Main & new thread has different thread ids"
                << std::endl;
    // Do some stuff in Main Thread
    std::cout << "Waiting for thread " << threadId << " to exit" << std::endl;
    // Wait for thread to exit, pass thread id as first argument
    err = pthread_join(threadId, NULL);
    // check if joining is sucessful
    if (err)
    {
        std::cout << "Failed to join Thread : " << strerror(err) << std::endl;
        return err;
    }
    std::cout << "Exiting Main" << std::endl;
    return 0;
}
Output :
Thread Id from Main function thread : 139687770216256
Thread Created with ID : 139687753377536
Both Main & new thread has different thread ids
Waiting for thread 139687753377536 to exit
Thread Id from thread function : 139687753377536
Exiting Main