C++ 多线程中的一个抛出异常

试了一下,和Java完全不同。

注意Java和C++对于多线程里面的一个线程抛出异常的影响,完全不同。

Java里面,对于主线程和其他线程完全不受影响;

C++里面,整个程序会退出,所有线程都会受影响。

 

Java的多线程与异常的关系,可以看这里:http://www.cnblogs.com/charlesblc/p/6175617.html

 

C++实验,代码如下:

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

using namespace std;

#define NUM_THREADS 5

void* proca(void*)
{
    printf("In ProcA\n");
    sleep(5);
    printf("ProcA: Try Throw...\n");
    throw 3;
}
void* procb(void*)
{
    printf("In ProcB\n");
    //printf("ProcA: Try Throw...\n");
    //throw 4;
}

int main()
{
    pthread_t tids[NUM_THREADS];
    printf("In Main Thread.\n");
    int ret = pthread_create( &tids[0], NULL, proca, NULL);
    printf("ProcA Started.\n");
    ret = pthread_create( &tids[1], NULL, procb, NULL);
    printf("ProcB Started.\n");

    sleep(10);
    printf("Start joining A\n");
    pthread_join(tids[0], NULL);
    printf("Start joining B\n");
    pthread_join(tids[1], NULL);

    return 0;
}

编译命令:

g++ -g -o try try.cpp -lpthread

运行:

$ ./try 
In Main Thread.
ProcA Started.
In ProcA
ProcB Started.
In ProcB
(注意这里停顿大约5秒)
ProcA: Try Throw...
terminate called after throwing an instance of 'int'
Aborted (core dumped)

调试core dump:

$ gdb -c core.13689 ./try
GNU gdb Red Hat Linux (6.3.0.0-1.96rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu"...Using host libthread_db library "/lib64/tls/libthread_db.so.1".

Reading symbols from shared object read from target memory...done.
Loaded system supplied DSO at 0x7fff9d3ff000
Core was generated by `./try'.
Program terminated with signal 6, Aborted.
Reading symbols from /lib64/tls/libpthread.so.0...done.
Loaded symbols for /lib64/tls/libpthread.so.0
Reading symbols from /usr/lib64/libstdc++.so.6...done.
Loaded symbols for /usr/lib64/libstdc++.so.6
Reading symbols from /lib64/tls/libm.so.6...done.
Loaded symbols for /lib64/tls/libm.so.6
Reading symbols from /lib64/libgcc_s.so.1...done.
Loaded symbols for /lib64/libgcc_s.so.1
Reading symbols from /lib64/tls/libc.so.6...done.
Loaded symbols for /lib64/tls/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
#0  0x0000003f0b02e2ed in raise () from /lib64/tls/libc.so.6
(gdb) bt
#0  0x0000003f0b02e2ed in raise () from /lib64/tls/libc.so.6
#1  0x0000003f0b02fa3e in abort () from /lib64/tls/libc.so.6
#2  0x0000003f0bfb1138 in __gnu_cxx::__verbose_terminate_handler () from /usr/lib64/libstdc++.so.6
#3  0x0000003f0bfaf166 in __cxa_call_unexpected () from /usr/lib64/libstdc++.so.6
#4  0x0000003f0bfaf193 in std::terminate () from /usr/lib64/libstdc++.so.6
#5  0x0000003f0bfaf293 in __cxa_throw () from /usr/lib64/libstdc++.so.6
#6  0x00000000004007ee in proca () at try.cpp:15
#7  0x0000003f0b90610a in start_thread () from /lib64/tls/libpthread.so.0
#8  0x0000003f0b0c5ee3 in clone () from /lib64/tls/libc.so.6
#9  0x0000000000000000 in ?? ()
(gdb) 

 

其中飘红的地方就是抛出异常的地方。

 

posted @ 2016-12-15 23:22  blcblc  阅读(7749)  评论(0编辑  收藏  举报