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

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

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

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

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

public member function

1. std::thread::join

void join();

The function returns when the thread execution has completed.
阻塞当前线程,直到线程完成其执行。主线程调用 std::thread::join 会阻塞等待子线程完成,确保所有资源正确释放,避免进程退出时资源泄露或段错误。

This synchronizes the moment this function returns with the completion of all the operations in the thread: This blocks the execution of the thread that calls this function until the function called on construction returns (if it hasn’t yet).
这会将该函数返回的时刻与线程中所有操作的完成同步:这会阻止调用该函数的线程的执行,直到构造时调用的函数返回 (如果尚未返回)。

After a call to this function, the thread object becomes non-joinable (std::thread::joinable) and can be destroyed (std::thread::~thread) safely.

2. Parameters

none

3. Return value

none

4. Examples

#include <iostream> 
#include <thread> 
#include <chrono> void pause_thread(int n) {std::this_thread::sleep_for(std::chrono::seconds(n));std::cout << "pause of " << n << " seconds ended" << std::endl;
}int main() {std::cout << "Spawning 3 threads..." << std::endl;std::thread t1(pause_thread, 1);std::thread t2(pause_thread, 2);std::thread t3(pause_thread, 3);std::cout << "Done spawning threads. Now waiting for them to join:" << std::endl;t1.join();t2.join();t3.join();std::cout << "All threads joined!" << std::endl;return 0;
}
Spawning 3 threads...
Done spawning threads. Now waiting for them to join:
pause of 1 seconds ended
pause of 2 seconds ended
pause of 3 seconds ended
All threads joined!
请按任意键继续. . .

5. Data races (数据竞争)

The object is modified.

Note that any operations on the thread object itself are not synchronized (unlike the operations within the thread it represents).

6. Exception safety (异常安全性)

Basic guarantee: if an exception is thrown by this member function, the thread object is left in a valid state.

If the call fails, a std::system_error exception is thrown:

exception typeerror conditiondescription
std::system_errorerrc::invalid_argumentThe thread object is not joinable
std::system_errorerrc::no_such_processThe thread object is not valid
std::system_errorerrc::resource_deadlock_would_occurThe current thread is the same as the thread attempted to join, or A deadlock was detected (implementations may detect certain cases of deadlock).

Note that if the thread represented by the object terminates with an uncaught exception, this cannot be caught by the current thread, and std::terminate is automatically called.

References

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

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

相关文章:

  • Window 部署 coze-stdio(coze 开发平台)
  • GAN/cGAN中到底要不要注入噪声
  • InfluxDB 与 MQTT 协议集成实践(二)
  • Element表格单元格类名动态设置
  • Linux网络
  • libomxil-bellagio移植到OpenHarmony
  • Ubuntu简述及部署系统
  • MybatisPlus-19.插件功能-通用分页实体
  • JDK 11.0.16.1 Windows 安装教程 - 详细步骤+环境变量配置
  • Day44 Java数组08 冒泡排序
  • AI与区块链Web3技术融合:重塑数字经济的未来格局
  • SpringSecurity实战:核心配置技巧
  • 【前端】【vscode】【.vscode/settings.json】为单个项目配置自动格式化和开发环境
  • 【C++基础】类型转换:static_cast/dynamic_cast 面试高频考点与真题解析
  • Spring Retry 异常重试机制:从入门到生产实践
  • ESP32学习-FreeRTOS队列使用指南与实战
  • 【多模态】天池AFAC赛道四-智能体赋能的金融多模态报告自动化生成part2-报告输出
  • Java面试实战:企业级性能优化与JVM调优全解析
  • 小白成长之路-Ansible自动化(一)
  • 将远程 main 分支同步到 develop 分支的完整指南
  • 【硬件】嵌入式软件开发(2)
  • STM32-USART串口实现接收数据三种方法(1.根据\r\n标志符、2.空闲帧中断、3.根据定时器辅助接收)
  • Pytest 参数化进阶:掌握 parametrize 的多种用法
  • HCIP---MGRE实验
  • 嵌入式硬件篇---ESP32稳压板
  • OpenLayers 综合案例-轨迹回放
  • LeetCode|Day27|70. 爬楼梯|Python刷题笔记
  • catkin_make与catkin build的关系与区别(使用catkin build的好处)
  • MGRE实验
  • 深入解析 Vue 3 中 v-model 与表单元素的绑定机制