std::thread::get_id

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

返回标识与 *this 关联的线程的 std::thread::id 类型值。

目录

参数

(无)

返回值

标识与 *this 关联的线程的 std::thread::id 类型值。若无关联的线程,则返回默认构造的 std::thread::id

示例

#include <iostream>
#include <thread>
#include <chrono>
 
void foo()
{
    std::this_thread::sleep_for(std::chrono::seconds(1));
}
 
int main()
{
    std::thread t1(foo);
    std::thread::id t1_id = t1.get_id();
 
    std::thread t2(foo);
    std::thread::id t2_id = t2.get_id();
 
    std::cout << "t1's id: " << t1_id << '\n';
    std::cout << "t2's id: " << t2_id << '\n';
 
    t1.join();
    t2.join();
}

可能的输出:

t1's id: 0x35a7210f
t2's id: 0x35a311c4

参阅

表示线程的 id
(公开成员类)
检查线程是否可合并,即潜在地运行于平行环境中
(公开成员函数)

版本历史

  • (当前 | 先前 2017年8月25日 (五) 06:08Fruderica讨论 | 贡献. . (1,072字节) (-611). . (撤销)
  • 当前 | 先前 2013年7月2日 (二) 11:35P12bot讨论 | 贡献 . . (1,683字节) (-40). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 18:32P12bot讨论 | 贡献 . . (1,723字节) (+257). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 03:32P12讨论 | 贡献 . . (1,466字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,466字节) (+1,466). . (Translated from the English version using Google Translate)