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) 

 

其中飄紅的地方就是拋出異常的地方。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM