线程标识符id可以通过thread::get_id()获得,若thread obejct没有和任何线程关联则返回一个NULL的std::thread::id表示没有任何线程。当前线程若想获得自己的id可以调用std::this_thread::get_id()。

thread::id对象可以被任意复制和比较。这里的比较语义是:若相等表示是同一个线程或者都没有线程,不等表示不同的线程。

 bool operator== (thread::id lhs, thread::id rhs) noexcept;
bool operator!= (thread::id lhs, thread::id rhs) noexcept;
bool operator< (thread::id lhs, thread::id rhs) noexcept;
bool operator>= (thread::id lhs, thread::id rhs) noexcept;
bool operator> (thread::id lhs, thread::id rhs) noexcept;
bool operator>= (thread::id lhs, thread::id rhs) noexcept

thread::id可以用于关联容器的key,可以用于排序,用于比较等用途。比如std::hash<std::thread::id>

主线程在启动子线程之前记录下自己的master_thread,然后每个子线程启动时都去比较这个ID,若不是则执行do_common_work(),主线程则执行do_master_thread_work(),这样就可以将主线程和子线程的工作统一到一个函数中,但是主线程和子线程的工作又不一样。

 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();
}
    thread::id可以作为关联容器的key,关联容器中可以根据key来存放线程的私有数据。

输出线程标识符std::cout<<std::this_thread::get_id();

 
 

thread::id的更多相关文章

  1. cuda中thread id

    //////////////////////////////////////////////////////////////////////////// // // Copyright 1993-20 ...

  2. How to Find Processlist Thread id in gdb !!!!!GDB 使用

    https://mysqlentomologist.blogspot.jp/2017/07/           Saturday, July 29, 2017 How to Find Process ...

  3. 根据 thread id 停止一个线程

    出自 https://github.com/Bogdanp/dramatiq/blob/master/dramatiq/middleware/threading.py#L62 thread_id = ...

  4. std::thread中获取当前线程的系统id

    std::thread不提供获取当前线程的系统id的方法,仅可以获取当前的线程id,但是我们可以通过建立索引表的方式来实现 std::mutex m; std::map<std::thread: ...

  5. 多线程爬坑之路-Thread和Runable源码解析

    多线程:(百度百科借一波定义) 多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提 ...

  6. Linux process vs thread

    Linux process vs thread Question I have a query related to the implementation of threads in Linux. L ...

  7. 漫谈c++11 Thread库之使写多线程程序

    c++11中最重要的特性之一就是对多线程的支持了,然而<c++ primer>5th却没有这部分内容的介绍,着实人有点遗憾.在网上了解到了一些关于thread库的内容.这是几个比较不错的学 ...

  8. 接收新信息,在会话中看不到(thread表数据插入/更新失败)

    分析原因:收到短信,sms表插入信息,触发器会自动更新thread表,更新失败导致一直有一条未读信息数量显示,但在会话列表中却看不到. (偶现,低概率. 解决方法:接收新信息插入后,立即查询threa ...

  9. 查找原始MySQL死锁ID

    转载地址:http://yueliangdao0608.blog.51cto.com/397025/1180917 如果遇到死锁了,怎么解决呢?找到原始的锁ID,然后KILL掉一直持有的那个线程就可以 ...

随机推荐

  1. 前段基础JavaScript

    JavaScript概述 JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEase.( ...

  2. poj 2242(已知三点求外接圆周长)

    The Circumference of the Circle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8310   ...

  3. spring quartz job autowired 出错 null pointer

    什么情况下才能用autowired? 当当前类属于spring IOC容器管时候就可以,比如在applicationContext.xml里有定义 就是说在spring上下文里能够找到 但是比如qua ...

  4. 微信小程序保存图片的方法

    1.xhtml代码 长按保存: <view class="img" catchlongpress='baocun'></view> 2.Js代码 baocu ...

  5. set注入

    顾名思义set注入必须要有set方法. 基本类型的注入.引用类型注入.List注入.Set注入.Map注入.Properties注入 public class person { private car ...

  6. 一句话木马与caidao

    实验吧有个试验环境:http://www.shiyanbar.com/experiment-course/experiment-course/vid/1812 菜刀的主要功能是用来连接一句话木马的,a ...

  7. HDU 1427 速算24点【数值型DFS】

    速算24点 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  8. RPD Volume 172 Issue 1-3 December 2016 评论03

    Using Stable Free Radicals to Obtain Unique and Clinically Useful Data In Vivo in Human Subjects Abs ...

  9. luogu P1064 金明的预算方案

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”.今 ...

  10. POJ 3177 Redundant Paths(边双连通分量)

    [题目链接] http://poj.org/problem?id=3177 [题目大意] 给出一张图,问增加几条边,使得整张图构成双连通分量 [题解] 首先我们对图进行双连通分量缩点, 那么问题就转化 ...