EmptyData EmptyData - 1 year ago 100
C++ Question

What happens when child thread crash and main wait for join?

In the following program I created a

thread1 which crash in function
. I am interested in what exactly happened for
command in

I ran below program and in exited normally by printing "complete". I dont know why?

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cstring>
#include <climits>
#include <cstdio>
#include <stdlib.h>
using namespace std;

void* func(void *data)
cout<<"Calling func"<<(long)(data)<<endl;
int *a;

int main( )
pthread_t thread1;
pthread_create(&thread1, 0 , &func, (void*)2);
pthread_join(thread1, NULL);


Answer Source

the threads operate mostly independent, meaning that each thread can use a signal-handler to catch a "crash"-signal without having the other threads to be killed aswell. Therefor signal-handlers needs to be added.

source: signal-manpage http://man7.org/linux/man-pages/man7/signal.7.html

A signal may be generated (and thus pending) for a process as a whole (e.g., when sent using kill(2)) or for a specific thread (e.g., certain signals, such as SIGSEGV and SIGFPE, generated as a consequence of executing a specific machine-language instruction are thread directed, as are signals targeted at a specific thread using pthread_kill(3)). A process-directed signal may be delivered to any one of the threads that does not currently have the signal blocked. If more than one of the threads has the signal unblocked, then the kernel chooses an arbitrary thread to which to deliver the signal.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download