深入学习c++--多线程编程(三)thread的两种死法
1. 生成了一个线程,需要告诉编译器是否管理
必须告诉编译器是不管理还是管理,否则直接down了
#include <iostream>
#include <thread>
#include <chrono>
#include <future>
#include <atomic>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <string>
#include <mutex>
using namespace std; void joinWorker()
{ } void detachWorker()
{
} class Obj {
public:
Obj() {
cout << "hello\n";
}
~Obj() {
cout << "world\n";
}
}; int main()
{
Obj obj;
thread j(joinWorker); //程序直接down了,析构函数都没有调用 return ;
}
1.1 可以通过join(),自己管理
int main()
{
Obj obj;
thread j(joinWorker); if (j.joinable())
j.join(); return ;
}
如果遇到异常,没有调用join,自己可以写一个析构调用join()
class ThreadGuard {
public:
ThreadGuard(thread& t) : m_thread(t) {
}
~ThreadGuard() {
if (m_thread.joinable())
m_thread.join();
}
private:
thread& m_thread;
};
1.2 通过detach(),不管理
detach适合不会出错,生命周期比整个程序短,不想管理的程序
#include <iostream>
#include <thread>
#include <chrono>
#include <future>
#include <atomic>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <string>
#include <mutex>
using namespace std; class Obj {
public:
Obj() {
cout << "hello\n";
}
~Obj() {
cout << "world\n";
}
}; void joinWorker()
{
} void detachWorker()
{
Obj obj;
} int main()
{
// Obj obj;
thread j(joinWorker);
thread w(detachWorker);
w.detach(); if (j.joinable())
j.join(); // w.join(); // w线程不能再join() return ;
}
如果程序执行时间长,则可能不会调用析构函数
void detachWorker()
{
Obj obj;
this_thread::sleep_for(chrono::seconds());
}
深入学习c++--多线程编程(三)thread的两种死法的更多相关文章
- java Thread编程(三) 同步的两种不同实现方式
1,创建需要同步的对象(方式一) package concurrency; public class Bank { private double amount; public Bank(double ...
- Java并发编程(一) 两种实现多线程的方法(Thread,Runnable)
Java中实现多线程的方法有两种: 继承Thread类和实现Runnable方法,并重写Run方法,然后调用start()方法启动线程.使用Runnable会比Thread要好很多,主要是以下三个原因 ...
- Linux程序设计学习笔记----多线程编程线程同步机制之相互排斥量(锁)与读写锁
相互排斥锁通信机制 基本原理 相互排斥锁以排他方式防止共享数据被并发訪问,相互排斥锁是一个二元变量,状态为开(0)和关(1),将某个共享资源与某个相互排斥锁逻辑上绑定之后,对该资源的訪问操作例如以下: ...
- java多线程总结:线程的两种创建方式及优劣比较
1.通过实现Runnable接口线程创建 (1).定义一个类实现Runnable接口,重写接口中的run()方法.在run()方法中加入具体的任务代码或处理逻辑. (2).创建Runnable接口实现 ...
- 1.java多线程_实现线程的两种方式
1.java多线程基本知识 1.1.进程介绍 不管是我们开发的应用程序,还是我们运行的其他的应用程序,都需要先把程序安装在本地的硬盘上.然后找到这个程序的启动文件, 启动程序的时候,其实是电脑把当前的 ...
- java web学习总结(二十九) -------------------JavaBean的两种开发模式
SUN公司推出JSP技术后,同时也推荐了两种web应用程序的开发模式,一种是JSP+JavaBean模式,一种是Servlet+JSP+JavaBean模式. 一.JSP+JavaBean开发模式 1 ...
- PMP知识点(二)——三点估算的两种方法对活动持续时间估算的影响和如何取舍
一.准备工作 活动持续时间的估算属于PMBOK中第六章项目时间管理中第五节6.6估算活动持续时间的内容. 三点估算是6.5和7.2(估算成本)中应用到的一种工具和技术.数据流向图参考如下: 其应用到的 ...
- UNIX环境编程学习笔记(28)——多线程编程(三):线程的取消
lienhua342014-11-24 1 取消线程 pthread 提供了pthread_cancel 函数用于请求取消同一进程中的其他线程. #include <pthread.h> ...
- APUE学习之多线程编程(三):线程属性、同步属性
一.线程属性 可以使用pthread_attr_t结构修改线程默认属性,并这些属性和创建的线程练习起来,可以使用pthread_att_init函数初始化pthread_attr_t结构,调 ...
随机推荐
- Qt编译出错 GL/gl.h:No such file or directory
系统:Ubuntu18.04 软件:QtCreator4.8 Qt5.9.8 编译Qt Widgets Application类型的项目时,出现错误,提示: GL/gl.h: No such file ...
- IDEA如何配置tomcat及建一个web项目
需求:项目工程可能别人已经建好了,我需要把项目导入到自己的IDEA并配置tomcat运行; 准备工作:1.本地的tomat服务器 2.IDEA工具 3.JDK 步骤: 1.点击Run -> Ed ...
- Eclipse经常出现未响应问题
修改eclipse.ini文件 -startupplugins/org.eclipse.equinox.launcher_1.5.0.v20180512-1130.jar--launcher.libr ...
- String,StringBuffer,StringBuilder区别(笔记)
String类被final修饰,创建的对象为不可变对象,属于字符串常量. 而StringBuffer与StringBuilder创建的属于字符串常量. StringBuffer的方法大多用了Synch ...
- 加速Github访问
Github 仓库的数据传输很慢,甚至可能导致仓库拉取失败.例如: remote: Enumerating objects: , done. remote: Counting objects: % ( ...
- Celery(异步任务,定时任务,周期任务)
1.什么是Celery Celery是基于Python实现的模块,用于异步.定时.周期任务的. 组成结构: 1.用户任务 app 2.管道broker 用于存储任务 官方推荐 redis/rabbit ...
- What is the syntax for a for loop in TSQL?
loop 报错 英 [luːp] 美 [lup] 口语练习 vi. 打环:翻筋斗 n. 环:圈:弯曲部分:翻筋斗 vt. 使成环:以环连结:使翻筋斗 syntax 报错 英 ['sɪntæks ...
- LeetCode(数据库)部门最高工资
), Salary int, DepartmentId int) )) Truncate table Employee ') ') ') ') Truncate table Department ', ...
- tensorflow API _ 4 (优化器配置)
"""Configures the optimizer used for training. Args: learning_rate: A scalar or `Tens ...
- LeetCode 1061. Lexicographically Smallest Equivalent String
原题链接在这里:https://leetcode.com/problems/lexicographically-smallest-equivalent-string/ 题目: Given string ...