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

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

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

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

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

public member function

1. std::thread::joinable

bool joinable() const noexcept;

Returns whether the thread (std::thread) object is joinable.

A thread (std::thread) object is joinable if it represents a thread of execution.

A thread (std::thread) object is not joinable in any of these cases:

  • if it was default-constructed (https://cplusplus.com/reference/thread/thread/thread/).
  • if it has been moved from (either constructing another thread object, or std::thread::operator=).
  • if either of its members join (std::thread::join) or detach (std::thread::detach) has been called.

2. Parameters

none

3. Return value

true if the thread is joinable.
false otherwise.

4. Examples

#include <iostream>
#include <thread>void forever() {std::cout << "void forever()" << std::endl;
}int main() {std::thread first;std::thread second(forever);std::cout << "Joinable after construction:\n" << std::boolalpha;std::cout << "first: " << first.joinable() << '\n';std::cout << "second: " << second.joinable() << '\n';if (first.joinable()) first.join();if (second.joinable()) second.join();std::cout << "Joinable after joining:\n" << std::boolalpha;std::cout << "first: " << first.joinable() << '\n';std::cout << "second: " << second.joinable() << '\n';return 0;
}
void forever()Joinable after construction:
first: false
second: trueJoinable after joining:
first: false
second: false
请按任意键继续. . .

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::joinable, https://cplusplus.com/reference/thread/thread/joinable/

http://www.lryc.cn/news/601973.html

相关文章:

  • 3.Linuxvim编辑器及快捷键的使用
  • 【奔跑吧!Linux 内核(第二版)】第4章:内核编译和调试
  • 进度条制作--Linux知识的小应用
  • Linux 基础命令大全
  • Petalinux生成文件的关系
  • 基于Java的KTV点歌系统的设计与实现
  • 8.c语言指针
  • LeetCode 76:最小覆盖子串
  • 区块链共识机制与联邦学习
  • ArKTS:List 数组
  • D3.js的力导向图使用入门笔记
  • Java面试实战:从基础到架构的全方位技术交锋
  • 系统整理Python的条件语句和常用方法
  • PostgreSQL并发控制
  • Android Framework知识点
  • 1439-素数环2
  • 《关于matplot中绘制图像中文字体乱码问题》
  • Python Pandas.cut函数解析与实战教程
  • 蜣螂优化算法的华丽转身:基于Streamlit的MSIDBO算法可视化平台
  • 《频率之光:共振之战》
  • 商品中心—1.B端建品和C端缓存
  • unity3dTextMeshPro 设置中文字体,解决中文显示为框或中文字后面带背景颜色的问题
  • RK3568 Linux驱动学习——Linux驱动开发准备工作
  • VUE2 学习笔记9 生命周期
  • 数学建模——灰色关联分析
  • 打开postman后一直空白
  • 3D碰撞检测系统 基于SAT算法+Burst优化(Unity)
  • 量子力学的基本假设
  • Java生态下的AI开发利器:LangChain4j与Spring AI深度对比与实战
  • 【时时三省】(C语言基础)指针数组和多重指针