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 新标准中引入了四个头文件来支持多线程编程,他们分别是< ...
随机推荐
- 两篇整合Activiti Modeler到业务系统
1. 无法进入editor. http://localhost:8080/YouPRJ/modeler/service/editor?id=2050,前提是这个id必需存在与act_re_model表 ...
- 如何提高iOS开发能力
① 阅读技术博客 在现在这个年代,博客的风头早已被微博盖过.但是每天早上上班后的半小时,一定是打开博客,其中有讨论技术的,也有总结个人的相关经历,读完后肯定会有所收获.阅读博客,还有一个原因是技术博客 ...
- Scriptable render pipeline unity
https://www.youtube.com/watch?v=zbjkEQMEShM LWRP https://blogs.unity3d.com/cn/2018/02/21/the-lightwe ...
- scala类型系统 type关键字
和c里的type有点像. scala里的类型,除了在定义class,trait,object时会产生类型,还可以通过type关键字来声明类型. type相当于声明一个类型别名: scala> t ...
- C# SQLite 创建数据库的方法增删查改语法和命令
SQLite介绍 SQLite是一个开源.免费的小型RDBMS(关系型数据库),能独立运行.无服务器.零配置.支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准. SQLite数据库官方主页 ...
- Node.js mm131图片批量下载爬虫1.01 增加断点续传功能
这里的断点续传不是文件下载时的断点续传,而是指在爬行页面时有时会遇到各种网络中断而从中断前的页面及其数据继续爬行的过程,这个过程和断点续传原理上相似故以此命名.我的具体做法是:在下载出现故障或是图片已 ...
- 【Spark】RDD操作具体解释2——值型Transformation算子
处理数据类型为Value型的Transformation算子能够依据RDD变换算子的输入分区与输出分区关系分为下面几种类型: 1)输入分区与输出分区一对一型 2)输入分区与输出分区多对一型 3)输入分 ...
- MyEclipse 集成 Gradle开发环境
一.上Grandle官网下载Gradle,地址:http://www.gradle.org/downloads 如果只是运行只下载gradle-2.6-bin.zip 就可以了,如果为了扩展开发的话就 ...
- 小白系列-免费广告路由器web认证设置(2)
要设置认证页面图片.须要到后台注冊一个帐号,绑定路由器. 路由器管理后台网址 http://115.29.12.130/router 第一步:自己主动获取一个路由器ID(上一篇文章中的路由器ID也要改 ...
- 今天科普一下 iOS马甲包审核以及常见审核问题
一.什么是马甲包 马甲包是利用App store 规则漏洞,通过技术手段,多次上架同一款产品的方法.马甲包和主产品包拥有同样的内容和功能,除了icon和应用名称不能完全一致,其他基本一致. 二. ...