std::thread “terminate called without an active exception”
最近在使用std::thread的时候,遇到这样一个问题:
std::thread t(func);
如果不使用调用t.join()就会遇到 "terminate called whithout an active exception",但是在使用boost:thread的时候却没遇到这个问题,google了一下,找到答案:
The trouble you are encountering is a result of the stopThread going out of scope on the stack. The C++ standard has the following to say about this:
30.3.1.3 thread destructor [thread.thread.destr]
~thread();
If joinable() then terminate(), otherwise no effects. [ Note: Either implicitly detaching or joining ajoinable() thread in its destructor could result in difficult to debug correctness (for detach) or performance (for join) bugs encountered only when an exception is raised. Thus the programmer must ensure that the destructor is never executed while the thread is still joinable. — end note ]
What this means is that you should not let threads go out of scope without first calling either join() ordetatch().
The way you describe it, you want the thread to go out of scope without joining so it will continue to run as your application runs. That requires a call to detach(). From there, I can only offer a little wisdom...
大意是说,在~thread();前没有调用join()则会遇到问题很难调试,如果不想调用join()等线程结束的话你可以调用detach().这样就不会遇到"terminate called whithout an active exception"
如下:
{
std::thread t(func);
t.detach();
}
std::thread “terminate called without an active exception”的更多相关文章
- 起thread时,运行报错terminate called without an active exception
I am getting a C++ error with threading: terminate called without an active exception Aborted How to ...
- terminate called without an active exception异常
在gcc4.4下,采用回调机制写了一个类似std::thread的线程类. 但是使用时却发生了核心已转移的错误. main函数调用的代码大致是 int main(int argc, char *arg ...
- C++ std::thread
std::thread Defined in header class thread The class thread represents a single thread of execution. ...
- Correct thread terminate and destroy
http://www.techques.com/question/1-3788743/Correct-thread-destroy Hello At my form I create TFrame a ...
- C++11 并发指南------std::thread 详解
参考: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice/blob/master/zh/chapter3-Thread/Int ...
- Effective Modern C++ Item 37:确保std::thread在销毁时是unjoinable的
下面这段代码,如果调用func,按照C++的标准,程序会被终止(std::terminate) void func() { std::thread t([] { std::chrono::micros ...
- std::thread使用
本文将从以下三个部分介绍C++11标准中的thread类,本文主要内容为: 启动新线程 等待线程与分离线程 线程唯一标识符 1.启动线程 线程再std::threada对象创建时启动.最简单的情况下, ...
- C++11 并发指南二(std::thread 详解)
上一篇博客<C++11 并发指南一(C++11 多线程初探)>中只是提到了 std::thread 的基本用法,并给出了一个最简单的例子,本文将稍微详细地介绍 std::thread 的用 ...
- C++11并发——多线程std::thread (一)
https://www.cnblogs.com/haippy/p/3284540.html 与 C++11 多线程相关的头文件 C++11 新标准中引入了四个头文件来支持多线程编程,他们分别是< ...
随机推荐
- express和json的调用
在express工程里,建立app.js var express = require('express'); var app = express(); //数据接口 var newsdata=[{ ' ...
- Oracle数据库查看用户状态
一.当前ORACLE用户的状态可查看视图DBA_USERS;一般情况下在使用的正常用户均处于OPEN状态. 1 SQL> select username,account_status from ...
- RES协议
MFC 通过HTML访问内部资源 资源导入JPG的图片,资源对应ID是137 <img src="res:/JPG/#137" width="100%" ...
- Docker从12升级到17ce
先卸载 yum remove docker* yum remove container-selinux--.el7.centos.x86_64 安装 sudo yum install -y yum-u ...
- Tex系列: pgfplots安装
(1) 上网下载最新宏包压缩包 http://sourceforge.net/projects/pgfplots/files/pgfplots/ (2)解压压缩包,把该包下的tex子目录拷贝至D:\ ...
- [转]Understanding Integration Services Package Configurations
本文转自:http://msdn.microsoft.com/en-us/library/cc895212.aspx Introduction With the 2008 release, SQL S ...
- Delphi XE8 TStyleBook的使用
Delphi XE8来了,FMX的性能有了巨大的提升,比如:XE7下ListBox上下滑动的卡顿已经不复存在,直接用xe8编译后,再上下划动ListBox,已经变的非常流畅.另外,也见到有网友说,通过 ...
- Linux中文乱码问题终极解决方法
方法一: 修改/root/.bash_profile文件,增加export LANG=zh_CN.GB18030该文件在用户目录下,对于其他用户,也必须相应修改该文件. 使用该方法时putty能显示中 ...
- 关于Django迁移出现问题
关于Django迁移出现问题 源码: #coding:utf- from django.db import models # Create your models here. class BookIn ...
- scrapy-splash抓取动态数据例子七
一.介绍 本例子用scrapy-splash抓取36氪网站给定关键字抓取咨询信息. 给定关键字:个性化:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信 ...