最近在使用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. jquery children()方法

    1.测试代码 <!DOCTYPE html> <html> <head> <script type="text/javascript" s ...

  2. synchronized-锁重入

    public class MyThread5_synchronized1 { /** * 父子类同步必须 都 使用synchronized关键字 */ static class Main { publ ...

  3. 理解Flow静态类型检查

    一.为什么在JavaScript中使用静态类型 了解静态类型的最快方法是将其与动态类型进行对比. 有静态类型参数的语言被称为静态类型语言. 另一方面,有动态类型参数的语言被称为动态类型语言.核心区别是 ...

  4. MySQL建表设置外键提示错误

    错误内容: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to ...

  5. 15个CSS3和jQuery的超棒页面过渡效果教程

    来源:GBin1.com CSS3和jQuery从根本上改变了网页设计和程序开发.通过CSS3和jQuery,设计员和开发者不需要太多的精力或编码,就可以创造出非常 美丽令人叹惊的效果,同时还可以令你 ...

  6. Swift教程之运算符

    import Foundation //4.复合赋值操作符 var a = 1 a += 2 //一元减运算符(一个数值前加了符号-,叫作一元减运算符) let three = 3 let minus ...

  7. Solidworks如何添加齿轮

    打开ToolBox,找到GB,动力传动,齿轮,正齿轮,然后拖放到绘图窗口(切记要在装配图里面弄,不是在单个零件里面弄)   设置齿轮的参数,一般只需要设置模数,齿数,面宽,类型,总长度(面宽就是有齿轮 ...

  8. JMeter 十五:函数以及变量

    参考:http://jmeter.apache.org/usermanual/functions.html 函数以及参数引用 JMeter 函数引用方式如下: ${__functionName(var ...

  9. Android开发 之 我的jar包引用方法

    1.在工程上名上右键->Build Path ->Configure Build Path 2.在Libraries选项卡中,选择右侧的Add External JARs,然后选择要导入的 ...

  10. 记一次MySQL找回用户数据

    事情经过 有天,我们公司外区的一个销售C说他8月3号以前的工作流记录找不到了.问清缘由,原来是更新了微信号(我们公司的工作流是基于企业微信开发的).经过分析,微信号和流程数据并没什么关系,所以初步得出 ...