Namespaces
Variants

std::this_thread:: get_id

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
get_id
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
Определено в заголовке <thread>
std:: thread :: id get_id ( ) noexcept ;
(начиная с C++11)

Возвращает id текущего потока.

Содержание

Параметры

(нет)

Возвращаемое значение

id текущего потока.

Пример

#include <chrono>
#include <iostream>
#include <syncstream>
#include <thread>
using namespace std::chrono_literals;
void foo()
{
    std::thread::id this_id = std::this_thread::get_id();
    std::osyncstream(std::cout) << "thread " << this_id << " sleeping...\n";
    std::this_thread::sleep_for(500ms);
}
int main()
{
    std::jthread t1{foo};
    std::jthread t2{foo};
}

Возможный вывод:

thread 140113018054400 sleeping...
thread 140113009661696 sleeping...

Смотрите также

возвращает идентификатор потока
(публичная функция-член std::thread )
Документация C для thrd_current