最近在使用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();
}

最新文章

  1. bash中使用mysql中的update命令
  2. struts2中web.xml的配置
  3. Excel 取得一定范围内最大的有值的行号
  4. ss + pac
  5. 几种Java NIO框架的比较(zz)
  6. 将cocos2dx项目从Visual Studio 迁移到 xcode
  7. Python的注释
  8. pugixml
  9. XHTML与HTML的差别
  10. 规范 : 过程 : login cookies sessionTimeOut
  11. spring boot 整合js css 静态文件
  12. mysql5.7安装(正确安装)实战
  13. 11G新特性 -- Expression Statistics
  14. Java8获取当前时间、新的时间日期类如Java8的LocalDate与Date相互转换、ZonedDateTime等常用操作包含多个使用示例、Java8时区ZoneId的使用方法、Java8时间字符串解析成类
  15. 控制器中获取Field值
  16. 启动和停止kafka 及kafka manager
  17. Gym - 100342J Triatrip (bitset求三元环个数)
  18. android开发 gradle 总结
  19. mxonline实战10,课程列表页,课程详情页1
  20. 【python 2.7】python读取json数据存入MySQL

热门文章

  1. 用latex写毕业论文
  2. scikit-learn入门学习记录
  3. Vuex内容解析和vue cli项目中使用状态管理模式Vuex
  4. 《Linux操作系统编译构建指南》
  5. Hadoop Maven pom文件示例
  6. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何在同一台PC上运行多个TwinCAT程序
  7. swift 使用匿名函数初始化属性
  8. 【Statistics】均值
  9. JavaScript | 对象详解
  10. JDBC:数据库操作:BLOB数据处理