thread::id
线程标识符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();
}
输出线程标识符std::cout<<std::this_thread::get_id();
thread::id的更多相关文章
- cuda中thread id
//////////////////////////////////////////////////////////////////////////// // // Copyright 1993-20 ...
- How to Find Processlist Thread id in gdb !!!!!GDB 使用
https://mysqlentomologist.blogspot.jp/2017/07/ Saturday, July 29, 2017 How to Find Process ...
- 根据 thread id 停止一个线程
出自 https://github.com/Bogdanp/dramatiq/blob/master/dramatiq/middleware/threading.py#L62 thread_id = ...
- std::thread中获取当前线程的系统id
std::thread不提供获取当前线程的系统id的方法,仅可以获取当前的线程id,但是我们可以通过建立索引表的方式来实现 std::mutex m; std::map<std::thread: ...
- 多线程爬坑之路-Thread和Runable源码解析
多线程:(百度百科借一波定义) 多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提 ...
- Linux process vs thread
Linux process vs thread Question I have a query related to the implementation of threads in Linux. L ...
- 漫谈c++11 Thread库之使写多线程程序
c++11中最重要的特性之一就是对多线程的支持了,然而<c++ primer>5th却没有这部分内容的介绍,着实人有点遗憾.在网上了解到了一些关于thread库的内容.这是几个比较不错的学 ...
- 接收新信息,在会话中看不到(thread表数据插入/更新失败)
分析原因:收到短信,sms表插入信息,触发器会自动更新thread表,更新失败导致一直有一条未读信息数量显示,但在会话列表中却看不到. (偶现,低概率. 解决方法:接收新信息插入后,立即查询threa ...
- 查找原始MySQL死锁ID
转载地址:http://yueliangdao0608.blog.51cto.com/397025/1180917 如果遇到死锁了,怎么解决呢?找到原始的锁ID,然后KILL掉一直持有的那个线程就可以 ...
随机推荐
- poj 2242(已知三点求外接圆周长)
The Circumference of the Circle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8310 ...
- [BZOJ3698] XWW的难题 网络流
3698: XWW的难题 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 533 Solved: 275[Submit][Status][Discus ...
- [libgdx游戏开发教程]使用Libgdx进行游戏开发(4)-素材管理
游戏中总是有大量的图像资源,我们通常的做法是把要用的图片做成图片集,这样做的好处就不多说了.直接来看怎么用. 这里我们使用自己的类Assets来管理它们,让这个工具类作为我们的资源管家,从而可以在任何 ...
- 通过Cookie来记住用户名出现乱码问题(URL编码)
在登录时,提交一个中文名的用户名到服务器并返回到客户端的Cookie中时, 这个过程会后台会报 java.lang.IllegalArgumentException (非法数据异常) -->在给 ...
- Codeforces Round #424 A(模拟)
#include<cstdio> ]; int main(){ scanf("%d",&n); ;i<=n;++i)scanf("%d" ...
- Python与数据结构[4] -> 散列表[1] -> 分离链接法的 Python 实现
分离链接法 / Separate Chain Hashing 前面完成了一个基本散列表的实现,但是还存在一个问题,当散列表插入元素冲突时,散列表将返回异常,这一问题的解决方式之一为使用链表进行元素的存 ...
- BigDecimal不整除异常
通过BigDecimal的divide方法进行除法时当不整除,出现无限循环小数时,就会抛异常的 异 常 :java.lang.ArithmeticException: Non-terminatin ...
- [USACO06DEC] Milk Patterns
题目描述 Farmer John has noticed that the quality of milk given by his cows varies from day to day. On f ...
- POJ 1236 Network of Schools(SCC)
[题目链接] http://poj.org/problem?id=1236 [题目大意] 给出一张有向图,问需要从几个起点出发才能遍历全图, 如果要求从任何一个点出发都能遍历全图,那么最少需要增加几条 ...
- 【块状树】bzoj3731 Gty的超级妹子树
带 加点 删边的块状树. 加点在 bzoj3720 说过. 删边其实就是块顶打标记,记录其属于哪棵树,防止在dfs搜集答案时跑到别的树上. 然后暴力把所在块拆开. 好像用邻接表存图,直接在vector ...