zoukankan      html  css  js  c++  java
  • 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
    ");
        sleep(5);
        printf("ProcA: Try Throw...
    ");
        throw 3;
    }
    void* procb(void*)
    {
        printf("In ProcB
    ");
        //printf("ProcA: Try Throw...
    ");
        //throw 4;
    }
    
    int main()
    {
        pthread_t tids[NUM_THREADS];
        printf("In Main Thread.
    ");
        int ret = pthread_create( &tids[0], NULL, proca, NULL);
        printf("ProcA Started.
    ");
        ret = pthread_create( &tids[1], NULL, procb, NULL);
        printf("ProcB Started.
    ");
    
        sleep(10);
        printf("Start joining A
    ");
        pthread_join(tids[0], NULL);
        printf("Start joining B
    ");
        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) 

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

  • 相关阅读:
    maven 的依赖包的版本更改之后,项目启动报错,相关联的资源没有在tomcat里面加载
    Maven更新后本地仓库jar后缀带有 lastUpdated
    spring+mybatis之声明式事务管理初识(小实例)
    java 详解类加载器的双亲委派及打破双亲委派
    jvm内存溢出区域和排查方法
    JVM中的新生代和老年代(Eden空间、两个Survior空间)
    什么情况下会发生堆内存溢出,栈内存溢出,结合实例说明
    投而死面试
    检查性异常
    多线程系列课程
  • 原文地址:https://www.cnblogs.com/charlesblc/p/6185268.html
Copyright © 2011-2022 走看看