美文网首页
2.5识别线程

2.5识别线程

作者: 常春藤上的蜗牛 | 来源:发表于2017-09-13 08:48 被阅读0次

线程识别类型是std::thread::id。
检索方式:

  • 通过调用std::thread对象的成员函数get_id()来直接获取。如果 std::thread 对象没有与任何执行线程相关联, 那么 get_id() 将返回 std::thread::type 默认构造值, 这个值表示“没有线程”。
  • 在当前线程中,调用std::this_thread::get_id()也可以获得线程表示。
    std::thread::id 对象可以自由的拷贝和对比;标识符就可以复用
    std::thread::id类型对象提供相当丰富的对比操作,允许成员将其当做为容器的兼职,或做排序,或做其他方式的比较。
std::thread::id master_thread;
void some_core_part_of_algorithm()
{
    if(std::this_thread::get_id()==master_thread)
    {
        do_master_thread_work();
    } 
    do_common_work();
}

相关文章

网友评论

      本文标题:2.5识别线程

      本文链接:https://www.haomeiwen.com/subject/yrcksxtx.html