C++11 : How to get a Thread ID ?

In this article we will discuss how to get thread Id in different scenarios.

Every thread has an unique Id associated with it. c++11 provides a type to store this id i.e.

std::thread::id

Objects of std::thread::id is comparable, copy-able and default implementation of std::hash() is also provided by the standard. Therefore, std::thread::id objects can be used as keys in both map and unordered_map.

std::thread::get_id()

std::thread provides a member function get_id() i.e.

std::thread::id get_id() const noexcept;

It returns the thread id of associated object.

Let’s use this function to fetch the thread id i.e.

Get thread ID from a Join-able Thread Object

Let’s create a thread i.e.

// Starting Thread
std::thread th(threadFunction);

Now get it’s thread id from thread object.

// Fetching thread ID from Thread Object using get_id() member function
std::thread::id threadID = th.get_id();

Get thread ID from a Detached Thread object

Let’s create a thread i.e.

// Starting Thread
std::thread dThObj(threadFunction);

Detach the thread from thread object i.e.

// Detached the thread
dThObj.detach();

Now thread object has no associated thread with it’s id. Therefore, get_id() on detached thread object will return default constructed value i.e.

// Fetching thread ID from Thread Object using get_id() member function
std::thread::id dThreadID = dThObj.get_id();

// Detached thread's get_id() function will return default constructed thread::id only
assert(dThreadID == std::thread::id());

Get current thread ID inside thread function

Inside a function, which is currently executed by some thread, we can access the current thread object through,

std::this_thread

So, to get the current thread ID inside thread function, we can call get_id() with this_thread i.e.

// Fetch the thread ID of the thread which is executing this function
std::thread::id threadID = std::this_thread::get_id();

The complete example is as follows,

#include <thread>
#include <iostream>
#include <assert.h>
#include <chrono>

using namespace std::chrono_literals;

void threadFunction()
{
	std::cout << "Func Start" << std::endl;

	// Fetch the thread ID of the thread which is executing this function
	std::thread::id threadID = std::this_thread::get_id();

	std::cout << "Inside Thread :: Thread ID : " << threadID << "\n";
	std::cout << "Func End" << std::endl;
}

int main()
{

	// Starting Thread
	std::thread th(threadFunction);

	// Fetching thread ID from Thread Object using get_id() member function
	std::thread::id threadID = th.get_id();

	// Join the Thread if its Joinable
	if (th.joinable())
		th.join();

	std::cout << "Thread from Main : " << threadID << std::endl;

	/** Fetching Thread ID from Detached Thread ****/

	// Starting Thread
	std::thread dThObj(threadFunction);

	// Detached the thread
	dThObj.detach();

	// Fetching thread ID from Thread Object using get_id() member function
	std::thread::id dThreadID = dThObj.get_id();

	// Detached thread's get_id() function will return default constructed thread::id only
	assert(dThreadID == std::thread::id());

	std::this_thread::sleep_for(2s);

	std::cout << "Thread from Main : " << dThreadID << std::endl;
	return 0;
}

Output:

Func Start
Inside Thread :: Thread ID : 139924979255040
Func End
Thread from Main : 139924979255040
Func Start
Inside Thread :: Thread ID : 139924979255040
Func End
Thread from Main : thread::id of a non-executing thread

To Compile the above example on linux use following command,

g++ –std=c++14 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