当前位置: 首页 > news >正文

C++ 多线程 std::thread::get_id

C++ 多线程 std::thread::get_id

  • 1. `std::thread::get_id`
  • 2. Parameters
  • 3. Return value
  • 4. Examples
  • 5. Data races (数据竞争)
  • 6. Exception safety (异常安全性)
  • References

https://cplusplus.com/reference/thread/thread/get_id/

public member function

1. std::thread::get_id

id get_id() const noexcept;

Returns the thread id.

If the thread object is joinable, the function returns a value that uniquely identifies the thread.

If the thread object is not joinable, the function returns a default-constructed object of member type std::thread::id.

2. Parameters

none

3. Return value

An object of member type std::thread::id that uniquely identifies the thread (if joinable), or default-constructed (if not joinable)

4. Examples

#include <iostream>
#include <thread>
#include <chrono>std::thread::id main_thread_id = std::this_thread::get_id();void is_main_thread() {if (main_thread_id == std::this_thread::get_id()) {std::cout << "This is the main thread." << std::endl;}else {std::cout << "This is not the main thread." << std::endl;}
}int main() {is_main_thread();std::cout << "Yongqiang Cheng" << std::endl;std::thread th(is_main_thread);th.join();return 0;
}
This is the main thread.
Yongqiang Cheng
This is not the main thread.
请按任意键继续. . .

5. Data races (数据竞争)

The object is accessed.

6. Exception safety (异常安全性)

No-throw guarantee: never throws exceptions.

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] std::thread::get_id, https://cplusplus.com/reference/thread/thread/get_id/

http://www.dtcms.com/a/300979.html

相关文章:

  • 数独求解器与生成器(回溯算法实现)
  • Python|OpenCV-实现对颜色进行检测(22)
  • PandasAI连接LLM进行智能数据分析
  • qt常用控件-06
  • 【人工智能】【Python】各种评估指标,PR曲线,ROC曲线,过采样,欠采样(Scikit-Learn实践)
  • PAT 甲级题目讲解:1010《Radix》
  • Spring之【Bean的生命周期】
  • [AI8051U入门第十一步]W5500-服务端
  • Linux实战:从零搭建基于LNMP+NFS+DNS的WordPress博客系统
  • (10)数据结构--排序
  • 设计模式(八)结构型:桥接模式详解
  • k8s的权限
  • Python队列算法:从基础到高并发系统的核心引擎
  • Cline与Cursor深度实战指南:AI编程助手的革命性应用
  • 【Canvas与标牌】优质资产六角星标牌
  • Java面试全方位解析:从基础到AI的技术交锋
  • 力扣刷题(第一百天)
  • 【多模态】天池AFAC赛道四-智能体赋能的金融多模态报告自动化生成part1-数据获取
  • Linux之shell脚本篇(三)
  • 从0开始学linux韦东山教程Linux驱动入门实验班(6)
  • Linux Shell 命令
  • LabVIEW人脸识别
  • k8s pod生命周期、初始化容器、钩子函数、容器探测、重启策略
  • Vue基础(25)_组件与Vue的内置关系(原型链)
  • ESP32-S3学习笔记<7>:GP Timer的应用
  • 力扣热题100----------41.缺少的第一个正数
  • JavaScript单线程实现异步
  • [ The Missing Semester of Your CS Education ] 学习笔记 shell篇
  • 浅谈如何解决多组件系统相互依赖、调用导致接口复杂问题
  • 深入理解Java内存与运行时机制:从对象内存布局到指针压缩