C中多线程和异常安全之间的紧张关系是什么?有良好的指导方针吗?由于未捕获的异常,线程是否终止?
解决方法@H_502_4@
我相信C标准没有提到多线程 – 多线程是一个平台特定的功能.
我不太清楚C标准在一般情况下对于未捕获的例外情况如何,但根据this page,平台定义会发生什么,您应该在编译器的文档中找到.
在一个快速而肮脏的测试中,我用g 4.0.1(i686-apple-darwin8-g -4.0.1是具体的),结果是terminate()被调用,这会杀死整个程序.我使用的代码如下:
#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'
我不太清楚C标准在一般情况下对于未捕获的例外情况如何,但根据this page,平台定义会发生什么,您应该在编译器的文档中找到.
在一个快速而肮脏的测试中,我用g 4.0.1(i686-apple-darwin8-g -4.0.1是具体的),结果是terminate()被调用,这会杀死整个程序.我使用的代码如下:
#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'