User Tools

Site Tools


c:c_threads:identify_different_threads

C - C++ Threads - Identify different threads

Unique ids are given to threads.


#include <iostream>
#include <string>
#include <thread>
#include <functional>
 
void Count()
{
  for (int i = 0; i < 100; i++)
  {
    std::cout << "counter at: " << i << std::endl;
  }
}
 
int main()
{
  std::thread t1(Count);
 
  // Get the ID of the t1 thread.
  std::thread::id t1id = t1.get_id();
  std::cout << t1id << std::endl;
 
  // Get the ID of the MAIN thread.
  std::thread::id mainid = std::this_thread::get_id();
  std::cout << mainid << std::endl;
 
  t1.detach();
 
  return 0;
}

NOTE: The ids can be determined by:

  • Calling get_id() member function for a specific thread.
  • Calling std::this_thread::get_id() for the currently executing thread.
c/c_threads/identify_different_threads.txt · Last modified: 2021/03/05 16:30 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki