get_id
描述 (Description)
它返回线程ID。
声明 (Declaration)
以下是std :: thread :: get_id函数的声明。
id get_id() const noexcept;
C++11
id get_id() const noexcept;
参数 (Parameters)
没有
返回值 (Return Value)
它返回线程ID。
异常 (Exceptions)
No-throw guarantee - 永不抛出异常。
数据竞争 (Data races)
访问该对象。
例子 (Example)
在下面的示例中为std :: thread :: get_id。
#include <iostream>
#include <thread>
#include <chrono>
void foo() {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
int main() {
std::thread sample(foo);
std::thread::id sample_id = sample.get_id();
std::thread sample2(foo);
std::thread::id sample2_id = sample2.get_id();
std::cout << "sample's id: " << sample_id << '\n';
std::cout << "sample2's id: " << sample2_id << '\n';
sample.join();
sample2.join();
}
输出应该是这样的 -
sample's id: 139887397730048
sample2's id: 139887389337344