编写多线程异常安全代码 [英] Writing Multithreaded Exception-Safe Code

查看:70
本文介绍了编写多线程异常安全代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,多线程与异常安全之间的关系是什么?有什么好的指导方针可以遵循吗?线程是否由于未捕获的异常而终止?

What are the tensions between multithreading and exception-safety in C++? Are there good guidelines to follow? Does a thread terminate because of an uncaught exception?

推荐答案

我相信C ++标准没有提及多线程-多线程是特定于平台的功能.

I believe the C++ standard does not make any mention of multithreading - multithreading is a platform-specific feature.

我不完全确定C ++标准对未捕获异常的总体看法,但是根据此页面,发生的事情是平台定义的,您应该在编译器的文档中查找.

I'm not exactly sure what the C++ standard says about uncaught exceptions in general, but according to this page, what happens is platform-defined, and you should find out in your compiler's documentation.

在我使用g ++ 4.0.1(具体来说是i686-apple-darwin8-g ++-4.0.1)进行的快速测试中,结果是调用了terminate(),这杀死了整个程序.我使用的代码如下:

In a quick-and-dirty test I did with g++ 4.0.1 (i686-apple-darwin8-g++-4.0.1 to be specific), the result is that terminate() is called, which kills the entire program. The code I used follows:

#include <stdio.h>
#include <pthread.h>

void *threadproc(void *x)
{
  throw 0;

  return NULL;
}

int main(int argc, char **argv)
{
  pthread_t t;
  pthread_create(&t, NULL, threadproc, NULL);

  void *ret;
  pthread_join(t, &ret);

  printf("ret = 0x%08x\n", ret);

  return 0;
}

g++ threadtest.cc -lpthread -o threadtest一起编译.输出为:

terminate called after throwing an instance of 'int'

这篇关于编写多线程异常安全代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆