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

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

C++ 多线程中的一个抛出异常的更多相关文章

  1. 重新想象 Windows 8 Store Apps (42) - 多线程之线程池: 延迟执行, 周期执行, 在线程池中找一个线程去执行指定的方法

    [源码下载] 重新想象 Windows 8 Store Apps (42) - 多线程之线程池: 延迟执行, 周期执行, 在线程池中找一个线程去执行指定的方法 作者:webabcd 介绍重新想象 Wi ...

  2. C# WPF 登录多线程中 “调用线程无法访问对象,因为另一个线程拥有该对象“

    造成这个错误的原因很多,以下是我遇到的 我的思路,开启一个线程A登录.因为服务器响应登录成功需要在主线程做一些操作,我这边需要用到主线程的窗口对象,我把窗口对象传到线程 A,直接用实例方法会有这个错误 ...

  3. [C#学习]在多线程中如何调用Winform[转]

    问题的产生: 我的WinForm程序中有一个用于更新主窗口的工作线程(worker thread),但文档中却提示我不能在多线程中调用这个form(为什么?),而事实上我在调用时程序常常会崩掉.请问如 ...

  4. c#多线程中Lock()关键字的用法小结

    本篇文章主要是对c#多线程中Lock()关键字的用法进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助     本文介绍C# lock关键字,C#提供了一个关键字lock,它可以把一段 ...

  5. python 多线程中的同步锁 Lock Rlock Semaphore Event Conditio

    摘要:在使用多线程的应用下,如何保证线程安全,以及线程之间的同步,或者访问共享变量等问题是十分棘手的问题,也是使用多线程下面临的问题,如果处理不好,会带来较严重的后果,使用python多线程中提供Lo ...

  6. 多线程中volatile关键字的作用

    原文链接:https://blog.csdn.net/xuwentao37x/article/details/27804169 多线程的程序是出了名的难编写.难验证.难调试.难维护,这通常是件苦差事. ...

  7. 多线程中sleep方法,简单介绍。

    一 是什么? package com.aaa.threaddemo; /* * 多线程中的sleep方法? * sleep 隶属于Thread的方法,顾名思义,让线程睡一会. 1 public sta ...

  8. Java多线程中的死锁问题

    Java程序基本都要涉及到多线程,而在多线程环境中不可避免的要遇到线程死锁的问题.Java不像数据库那么能够检测到死锁,然后进行处理,Java中的死锁问题,只能通过程序员自己写代码时避免引入死锁的可能 ...

  9. 在ASP.NET Core中实现一个Token base的身份认证

    注:本文提到的代码示例下载地址> How to achieve a bearer token authentication and authorization in ASP.NET Core 在 ...

随机推荐

  1. C++中new和delete来创建和释放动态数组

    在C++编程中,使用new创建数组然后用delete来释放. 一.创建并释放一维数组 #include<iostream> using namespace std; int main() ...

  2. android技术总结

    1.要做一个尽可能流畅的ListView,你平时在工作中如何进行优化的? ①Item布局,层级越少越好,使用hierarchyview工具查看优化. ②复用convertView ③使用ViewHol ...

  3. Behavior Designer中的内置消息机制

    最近在用Behavior Designer,其中需要用到消息机制,看了一下其中自带了这套东西 注册 Owner.RegisterEvent<string>("Message&qu ...

  4. tinyhttpd服务器源码学习

    下载地址:http://sourceforge.net/projects/tinyhttpd/ /* J. David's webserver */ /* This is a simple webse ...

  5. Spring的BeanFactoryPostProcessor和BeanPostProcessor

    转载:http://blog.csdn.net/caihaijiang/article/details/35552859 BeanFactoryPostProcessor和BeanPostProces ...

  6. 放松时刻——C#分割字符串

    让我们来练习一下字符串的分割~把话倒过来说: private void change_button_Click(object sender, EventArgs e) { var after_text ...

  7. C语言中'\0'与'\n'

    '\0'表示ASCII编号为0的字符,在C语言中最常用于代表字符串结束的标志.'\n'表示ASCII编号为13的字符,代表回车键,输出这个字符就会换一行. '\0'作为字符串的结束标志,本身会占用一个 ...

  8. 在Struts2中配置Action

    在Struts2中配置Action <package>: 1.定义Action使用<package>标签下的<action>标签完成,一个<package&g ...

  9. 第四课 Activity

    1.Activity生命周期 1.1 创建-->启动-->获取焦点-->running-->失去焦点-->停止-->销毁.(一个正常activity的生命周期) 1 ...

  10. js写个日历

    其实我是一个对时间和日期不怎么感兴趣的人,小学的时候感觉时间或者日期那块就让我很晕,因为有时候是100进制有时候是60进制,搞的我对日历一直很不感兴趣,最近不知道为什么想写一个日历了,可想而知,这个玩 ...