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

In this article we will discuss how to get thread id of a pthread and compare different thread ids.

How to get thread id of current thread

Use pthread_self() to get the current thread id i.e.

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

It returns the thread id as pthread_t object for the calling thread. As main function is also a thread, so we can also call pthread_self() form main function too. pthread_self() never fails and always returns the thread id.

Let’s see how to use it,

// Get thread Id of calling thread
pthread_t thId = pthread_self();

How to get thread id while creating thread

When we create a thread using pthread_create(), we pass the pointer of pthread_t as first argument. When thread is created it is set to thread id i.e.

// Thread id
pthread_t threadId;

// Create a thread that will call function threadFunc() as thread function. Also
// pass 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);

Comparing 2 thread id (pthread_t) using pthread_equal

As pthread_t can be a structure, therefore we should not compare using == operator. POSIX provides a function pthread_equal() to compare 2 pthread_t i.e.

#include <pthread.h>

int pthread_equal(pthread_t t1, pthread_t t2);

Lets see how to use it,

//Compare Main thread Id and newly created thread id
bool result = pthread_equal(mainThreadId, threadId);

It returns 0 in case of failure and 1 in case they are equal.

Checkout complete example as follows,

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>

#include <unistd.h>

void * threadFunc(void * arg)
{
	// Get thread Id of calling thread
	pthread_t thId = pthread_self();
	std::cout << "Thread Id from thread function : " << thId << std::endl;
	return NULL;
}
int main()
{

	// Getting thread id of main function
	pthread_t mainThreadId = pthread_self();
	std::cout << "Thread Id from Main function thread : " << mainThreadId
			<< std::endl;

	/***** Create a New Thread ******/

	// Thread id
	pthread_t threadId;

	// Create a thread that will call function threadFunc() as thread function. Also
	// pass 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

To compile the above code use following command,

g++ example.cpp -lpthread

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top