One throw exception in multiple threads of C + +

Source: Internet
Author: User
Tags throw exception

Try it, it's completely different from Java.

Note that Java and C + + have an entirely different effect on a thread-throwing exception in multiple threads.

Java inside, for the main thread and other threads are completely unaffected;

In C + +, the entire program exits and all threads are affected.

Java's multi-threading relationship with exceptions can be seen here: http://www.cnblogs.com/charlesblc/p/6175617.html

C + + experiment with the following code:

#include <stdio.h>#include<stdlib.h>#include<unistd.h>#include<pthread.h>using namespace std; #define Num_threads5void* PROCA (void*) {printf ("In proca\n"); Sleep (5); printf ("Proca:try throw...\n"); Throw3;}void* PROCB (void*) {printf ("In procb\n"); //printf ("Proca:try throw...\n"); //throw 4;}intMain () {pthread_t tids[num_threads]; printf ("In Main thread.\n"); intret = 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); return0;}

Compile command:

Try try. Cpp-lpthread

Run:

$./Try' int 'aborted (core dumped)

Debug Core Dump:

$ gdb-c core.13689./TryGNU gdb Red Hat Linux (6.3.0.0-1. 96rh) Copyright2004Free software Foundation, inc.gdb are free software, covered by the GNU general public License, and your arewelcome to Change it and/or distribute copies of it under certain conditions. Type"Show copying"To see the conditions. There is absolutely no warranty forGdb. Type "Show Warranty" fordetails. This GDB is configured as"X86_64-redhat-linux-gnu" ... Using host libthread_db Library "/lib64/tls/libthread_db.so.1". Reading symbols from Gkfx object read from Target Memory...done. Loaded system supplied DSO at0x7fff9d3ff000Core is 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.0Reading symbols from/usr/lib64/libstdc++.so.6... done . Loaded symbols for/usr/lib64/libstdc++.so.6Reading symbols from/lib64/tls/libm.so.6... done . Loaded symbols for/lib64/tls/libm.so.6Reading symbols from/lib64/libgcc_s.so.1... done . Loaded symbols for/lib64/libgcc_s.so.1Reading symbols from/lib64/tls/libc.so.6... done . Loaded symbols for/lib64/tls/libc.so.6Reading 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)

Where the red is the place where the exception is thrown.

One throw exception in multiple threads of C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.