最近在使用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”的更多相关文章

  1. 起thread时,运行报错terminate called without an active exception

    I am getting a C++ error with threading: terminate called without an active exception Aborted How to ...

  2. terminate called without an active exception异常

    在gcc4.4下,采用回调机制写了一个类似std::thread的线程类. 但是使用时却发生了核心已转移的错误. main函数调用的代码大致是 int main(int argc, char *arg ...

  3. C++ std::thread

    std::thread Defined in header class thread The class thread represents a single thread of execution. ...

  4. Correct thread terminate and destroy

    http://www.techques.com/question/1-3788743/Correct-thread-destroy Hello At my form I create TFrame a ...

  5. C++11 并发指南------std::thread 详解

    参考: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice/blob/master/zh/chapter3-Thread/Int ...

  6. Effective Modern C++ Item 37:确保std::thread在销毁时是unjoinable的

    下面这段代码,如果调用func,按照C++的标准,程序会被终止(std::terminate) void func() { std::thread t([] { std::chrono::micros ...

  7. std::thread使用

    本文将从以下三个部分介绍C++11标准中的thread类,本文主要内容为: 启动新线程 等待线程与分离线程 线程唯一标识符 1.启动线程 线程再std::threada对象创建时启动.最简单的情况下, ...

  8. C++11 并发指南二(std::thread 详解)

    上一篇博客<C++11 并发指南一(C++11 多线程初探)>中只是提到了 std::thread 的基本用法,并给出了一个最简单的例子,本文将稍微详细地介绍 std::thread 的用 ...

  9. C++11并发——多线程std::thread (一)

    https://www.cnblogs.com/haippy/p/3284540.html 与 C++11 多线程相关的头文件 C++11 新标准中引入了四个头文件来支持多线程编程,他们分别是< ...

随机推荐

  1. Node.js中的express框架获取参数

    express获取参数有三种方法: req.query  适合 http://localhost:3000/form?num=8888 req.body   适合http://localhost:30 ...

  2. es6,async简单总结

    1.简单来讲就是把函数变为异步操作的 async function demo() { let result = Math.random(); console.log(result); } 2.asyn ...

  3. Codeforces Round #313 (Div. 1) Gerald&#39;s Hexagon

    http://codeforces.com/contest/559/problem/A 题目大意:按顺序给出一个各内角均为120°的六边形的六条边长,求该六边形能分解成多少个边长为1的单位三角形. 解 ...

  4. 新手遇到的问题:Easy UI的对话框老是在页面载入完毕后自己主动弹出

    因为是第一次接触Easy UI,还不是非常熟悉,尝试了一下对话框功能,还是非常不错的.但问题是页面载入完毕后.全部的对话框都自己主动弹出来了,百度了好久,也没有详细说明确的,貌似别人都没有这个问题哦 ...

  5. git命令01

    1.了解git工具产生的背景知识.git 是什么? 目前它是一种分布式版本控制系统.那什么又是版本控制系统? 一种能自动帮助记录每次文件的改动,不仅仅是记录自己对文件的修 改变化,而且可以记录其他人对 ...

  6. web.xml文件的作用及基本配置

    Java的web工程中的web.xml文件有什么作用呢?它是每个web工程都必须的吗? 一个web中完全可以没有web.xml文件,也就是说,web.xml文件并不是web工程必须的. 那什么时候需要 ...

  7. third-maximum-number

    https://leetcode.com/problems/third-maximum-number/ // 开始我以为相同的也占一位,比如5,3,3,2,得出3,但是答案是需要2 public cl ...

  8. vue-cli 配置路由之间跳转传递参数

    1.有2种方式去传参,如下代码: <template> <div> <div>这里是首页</div> <router-link :to=" ...

  9. yaha分词

    yaha分词:https://github.com/jannson/yaha

  10. 使用MyEclipse 2014构建Maven项目的两种方法

    前提: MyEclipse已配置Maven,具体步骤见http://blog.csdn.net/haishu_zheng/article/details/51492491 方法一: 1 File--& ...