试了一下,和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)

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

最新文章

  1. What is a watch descriptor
  2. IOS开发-视频,音频,录音简单总结
  3. NOR Flash擦写和原理分析 (二)
  4. “我爱淘”冲刺阶段Scrum站立会议7
  5. [Quick-x]制作新手引导高亮区域方法之一:混合模式
  6. codeforces 664A Complicated GCD
  7. Linux下sqlite的安装与使用
  8. ios CAF音频转换为MP3
  9. java学习笔记 (7) —— 实现简单的上传处理
  10. 用elasticsearch索引mongodb数据
  11. iOS多线程开发之离不开的GCD(上篇)
  12. params修饰符
  13. [十四]基础类型之StringBuffer 与 StringBuilder对比
  14. TensorFlow 图片resize方法
  15. [iptables] 如何用iptables管理桥接模式下的设备
  16. python 小程序,打印数字
  17. sass和less
  18. [React] 01 - Intro: javaScript library for building user interfaces
  19. Gunicorn设计部分的翻译
  20. BFC特性 形成BFC

热门文章

  1. 理解 Android Build 系统
  2. 如何在html添加一个搜索框和一个按钮?
  3. CallableAndFuture
  4. Mix and Build(简单DP)
  5. #ifdef __cplusplus extern &quot;C&quot; { #endif
  6. 基于Node的PetShop,RESTful API以及认证
  7. RabbitMQ系列二(构建消息队列)
  8. afxmessagebox和messagebox
  9. Duilib创建窗口双击标题栏禁止窗口最大化
  10. 函数式编程之block