EmptyData EmptyData - 1 month ago 13
C++ Question

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

In the following program I created a

pthread_t
thread1 which crash in function
func()
. I am interested in what exactly happened for
pthread_join
command in
main()
.

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<pthread.h>
#include <stdlib.h>
using namespace std;

void* func(void *data)
{
cout<<"Calling func"<<(long)(data)<<endl;
int *a;
cout<<a[2]<<endl;
pthread_exit(0);
}

int main( )
{
pthread_t thread1;
pthread_create(&thread1, 0 , &func, (void*)2);
pthread_join(thread1, NULL);
cout<<"complete"<<endl;

}

Answer

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.

Comments