文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Cancellation & C++ Exception

Cancellation & C++ Exception

时间:2011-04-27  来源:Robbin

最近在关注Linux下的C++多线程编程,感觉是处处是地雷 我做了一些笔记,这篇是关注 pthread_cancel & c++ exception部分的。 1. Death of Thread 线程的死亡有几种原因 a. 自然死亡 - 线程完成了其执行的任务,即执行到入口函数的末尾并返回。 b. 自杀 - 线程可能因为某些内部错误,选择了自杀,即调用pthread_exit() c.  谋杀 - 被某个知道其tid的线程,调用pthread_cancel杀死 今天只说谋杀这个情况 2. pthread_cancel & exception 这两个东东粗看貌似没有任何关联,但Ulrich Drepper告诉我们,事情不是这样的:“In NPTL thread cancellation is implemented using exceptions. ” 一旦调用了pthread_cancel(), 被谋杀者所在线程会抛出一个异常,通过栈展开(stack unwinding)来析构对象。 通常这没有丝毫问题,但如果碰上这样的代码 try {     // do something } catch (...) // mean catch all exception {      // do something } catch(...)无法处理cancel引起的exception,进程会直接coredump FATAL: exception not rethrown Aborted (core dumped) 解决方法很简单,重新抛出即可 try {     // do something } catch (...) // mean catch all exception {      // do something      throw; // re-throw } 如果你非要catch住这个exception,也不是没有办法,但这并无什么特别的用处,也违背了C++ Exception处理的原则 总结: 1. 其实,最好不要用exception 2. 如果要用exception,那就最好不要用pthread_cancel 3. 如果非要用,那就老老实实re-throw exception吧(在catch all这种情况) 至于C,则有cancellation point,回头再说。 Ref 1. http://udrepper.livejournal.com/21541.html 2. http://blog.csdn.net/Solstice/archive/2011/02/12/6181488.aspx 3. https://www.lsgalilei.org/glibc-doc/Cancellation.html
相关阅读 更多 +
排行榜 更多 +
无限Fps

无限Fps

飞行射击 下载
幸存者时间僵尸

幸存者时间僵尸

飞行射击 下载
金属兄弟Metal Brother

金属兄弟Metal Brother

冒险解谜 下载