std::this_thread::get_id is an in-built function in C++ std::thread. It is an observer function which means it observes a state and then returns the corresponding output. Below is the syntax

std::thread::id get_id() noexcept; (since C++11)

It returns object of member type thread::id which uniquely identifies the thread (if joinable), or default-constructed (if not joinable).

Example

Below example shows how to find thread id and convert the thread id into string. Before accessing the id of thread, lock is acquired so that thread id is belongs to the correct thread.

Similar to C++, Boost library also provides API to find the thread id.

#include <iostream>
#include <thread>
#include <mutex>

std::mutex g_display_mutex;

std::string getThreadId()
{
  // Accquire lock
  g_display_mutex.lock();

  // For C++, use std::this_thread::get_id();
  boost::thread::id id0 = boost::this_thread::get_id();

  // Release lock
  g_display_mutex.unlock();

  // Convert thread id to string
  std::ostringstream oss;
  oss << id0;

  return oss.str();
}