C++多线程detach函数使用
C++多线程和detach相对的函数有join,join的意思是父线程等待子线程结束,而detach的含义是主线程和子线程相互分离,互不干扰,这样看起来没什么问题,但是在某些条件下需要特别谨慎。
例如当子线程中有while循环时,如果主线程退出,进程结束,子线程也会退出,但是子线程中申请的资源不一定会释放。请看如下例子:
使用join
#include <iostream>
#include <thread>
#include <windows.h>
#include <fstream>
void *childThread(void *arg);
using namespace std;class Resource {
public:Resource(int n) {this->n_ = n; fstream outfile(".\\out.txt", ios::app);if (!outfile.is_open()){std::cout << "Can not open file" << std::endl;}outfile << "Create Resource." << endl;outfile.close();}virtual ~Resource() {fstream outfile(".\\out.txt", ios::app);if (!outfile.is_open()){std::cout << "Can not open file" << std::endl;}outfile << "free Resource." << endl;outfile.close();}
private:int n_;};int main(int argc, char **argv) {std::thread t1 = std::thread(childThread, nullptr);Sleep(1000);t1.join();return 0;
}void *childThread(void *arg) {Resource r(5);while (true) {Sleep(60 * 1000);}return nullptr;}
主线程一直等待子线程结束,而子线程是一个while循环,程序一直等待,正常。
使用detach
#include <iostream>
#include <thread>
#include <windows.h>
#include <fstream>
void *childThread(void *arg);
using namespace std;class Resource {
public:Resource(int n) {this->n_ = n; fstream outfile(".\\out.txt", ios::app);if (!outfile.is_open()){std::cout << "Can not open file" << std::endl;}outfile << "Create Resource." << endl;outfile.close();}virtual ~Resource() {fstream outfile(".\\out.txt", ios::app);if (!outfile.is_open()){std::cout << "Can not open file" << std::endl;}outfile << "free Resource." << endl;outfile.close();}
private:int n_;};int main(int argc, char **argv) {std::thread t1 = std::thread(childThread, nullptr);Sleep(1000);t1.detach();return 0;
}void *childThread(void *arg) {Resource r(5);while (true) {Sleep(60 * 1000);}return nullptr;}
请看使用detach后,进程可以退出,不会卡住,但是out.txe文件中只有"Create Resource."没有 "free Resource.",说明定义的变量r没有析构,可能存在资源未释放的情况。
代码稍加修改如下:
#include <iostream>
#include <thread>
#include <windows.h>
#include <fstream>
void *childThread(void *arg);
using namespace std;class Resource {
public:Resource(int n) {this->n_ = n; fstream outfile(".\\out.txt", ios::app);if (!outfile.is_open()){std::cout << "Can not open file" << std::endl;}outfile << "Create Resource." << endl;outfile.close();}virtual ~Resource() {fstream outfile(".\\out.txt", ios::app);if (!outfile.is_open()){std::cout << "Can not open file" << std::endl;}outfile << "free Resource." << endl;outfile.close();}
private:int n_;};int main(int argc, char **argv) {std::thread t1 = std::thread(childThread, nullptr);Sleep(5000);t1.detach();return 0;
}void *childThread(void *arg) {Resource r(5);Sleep(3000);return nullptr;}
因为子线程sleep3秒,主线程sleep 5秒,所以定义的变量r的构造和析构打印都有,简言之,用detach需要特别谨慎,避免有资源未释放情况发生。
C++多线程detach函数使用相关推荐
- R语言attach函数、detach函数(全局注册或者全局解除)实战
R语言attach函数.detach函数(全局注册或者全局解除)实战 目录 R语言attach函数.detach函数(全局注册或者全局解除)实战 #基本语法 # 仿真数据 # 如果没有attach就直 ...
- ROS——多线程callback函数
多线程回调函数 ROS默认一个线程,也就是如果有多个订阅者对象,回调函数会一个接一个执行,对于一般的数据处理来说,一个线程可能已经足够执行多个回调函数并且看不出来阻塞的迹象,但是对于点云等大体量的数据 ...
- 详解PyTorch中的copy_()函数、detach()函数、detach_()函数和clone()函数
参考链接: copy_(src, non_blocking=False) → Tensor 参考链接: detach() 参考链接: detach_() 参考链接: clone() → Tensor ...
- java 多线程 函数_Java多线程--同步函数
/* 需求: 银行有一个金库 有两个储户分别存300元 每次存100元,存3次 目的:该程序是否有安全问题,如果有,如何解决? 如何找问题(很重要) 1.明确哪些代码是多线程运行代码 2.明确共享数据 ...
- Attach()和Detach()函数
一.Windows对象和MFC对象的区别? MFC对象实际上并没有把整个Windows对象都包装在其中. 对于窗口:MFC对象它只是有一个窗口句柄而已,这个窗口句柄如果指向一个实际存在的窗口对象(窗口 ...
- QT多线程run函数不能使用信号与槽
一.问题描述 今天遇到一个问题,我在一个子线程中定义一个信号与槽函数,然后直接连接,最后会报错Socket notifiers cannot be enabled or disabled from a ...
- c#多线程实现函数同步运行
(转载请注明出处:http://blog.csdn.net/buptgshengod) 方法比较笨,以后加深了解再改进吧. 我们假设有方法run1()和run2(),耗时都比较大,实现他们同步运行将大 ...
- MFC Attach()函数和Detach()函数
一.Windows对象和MFC对象的区别? MFC对象实际上并没有把整个Windows对象都包装在其中. 对于窗口:MFC对象它只是有一个窗口句柄而已,这个窗口句柄如果指向一个实际存在的窗口对象(窗口 ...
- POSIX多线程API函数
创建 int pthread_create(pthread_t* tidp,const pthread_attr_t* attr,void* (*start_rtn)(void*), void* ar ...
- pytorch torch.detach函数(返回一个新的`Variable`,从当前图中分离下来的)
detach()[source] Returns a new Variable, detached from the current graph. 返回一个新的Variable,从当前图中分离下来的. ...
最新文章
- 15年来这8门编程语言位置十分稳定,C#从低谷开始爬升
- MySQL-8.0 | 数据字典最强解读
- 有20万3年不用,怎样理财呢?
- 程序员之路:python3+PyQt5+pycharm桌面GUI开发
- 《Redis开发与运维》学习第五章
- 梯度、散度、旋度、拉普拉斯算子
- mysql 不等于 优化_Mysql优化
- 《第五项修炼》,读后感
- 【观察】星环科技重构数据云平台,持续释放数据红利和价值
- 经常使用网页播放器代码
- ratingbar 的使用
- 做完这套面试题,你才敢说懂Excel
- 设置IP代理错误:“[WinError 10061] 由于目标计算机积极拒绝,无法连接”解决办法
- 教你路由器端口映射设置方法
- sublime的自动保存设置
- Github建仓传码
- java咖啡机 加入清洗剂_如何清洗咖啡机—咖啡机的清洗方法介绍
- java.sql.SQLException: Field ‘habit_id‘ doesn‘t have a default value
- 华为metro1000描述,optix metro1000参数-华讯佳科技
- 强强合作,替代钉盘/微盘,企业实现低成本扩容
热门文章
- 物联网设备OTA软件升级之:升级包下载过程之旅
- 计算机专业硬件面试题目,硬件工程师面试试题
- 10大协作办公工具:实现团队工作效率最大化
- 初识instantRun
- 驱动miniPCIE网络模块EC20硬件电路详解
- php+aira2+ffmpeg下载m3u8文件并保存成mp4
- 5G学习笔记之RRC_IDLE/RRC_INACTIVE态UE功能概述
- JSON字符串如何转化成对象?
- python cls方法_Python 中的 __new__(cls)方法详解
- DAS、NAS、SAN三种存储架构比较