format/formatid.cpp

The following code example is taken from the book
C++23 - The Complete Guide by Nicolai M. Josuttis, Leanpub, 2026
The code is licensed under a Creative Commons Attribution 4.0 International License. Creative Commons License

// raw code

#include <print>
#include <thread>

int main()
{
  auto printThreadID = [] {
    std::println("We are in thread {:15}", std::this_thread::get_id());
  };

  // print main thread ID:
  printThreadID();

  // starts threads that print their thread ID:
  std::jthread t1{printThreadID};
  std::jthread t2{printThreadID};

  // wait for the end of the threads:
  t1.join();
  t2.join();
}