C++多线程库的常用函数 std::this_thread::get_id()

格式:函数 + 头文件 + 用例 + 解释说明

 

函数:
std::this_thread::get_id()

头文件:
<thread>


用例:
std::thread::id master_thread = std::this_thread::get_id();

 

另一种获取线程标识符 id 的办法:

线程标识类型为std::thread::id

可以通过调用std::thread对象的成员函数get_id()来直接获取。
如果std::thread对象没有与任何执行线程相关联,get_id()将返回std::thread::type默认构造值,这个值表示“无线程”。

 

 

练习代码:

#include <QCoreApplication>
#include <thread>
#include <iostream>

struct run{
    run(short num):m_num(num){}
    void operator()(){
        std::cout<<"run num is "<<m_num<<std::endl;
    }
private:
    short m_num;
};
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    std::thread::id id = std::this_thread::get_id();
    std::cout<<"this thread id is "<<id<<std::endl;

    std::thread t(run(100));
    std::cout<<"thread t id is "<<t.get_id()<<std::endl;
    t.join();
    return a.exec();
}

 

输出结果:

C++多线程库的常用函数 std::this_thread::get_id()

 

原文链接: https://www.cnblogs.com/azbane/p/15382699.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++多线程库的常用函数 std::this_thread::get_id()

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/398268

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月12日 上午9:35
下一篇 2023年4月12日 上午9:36

相关推荐