What happens when the thread of the child threads and the main connection queue fail?

In the following program, I created the thread pthread_t thread1, which crashed in the func() function. I am interested in what exactly happened for the pthread_join command in main() .

I ran below the program and exited normally, typing "complete". I do not 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; } 
+7
c ++ multithreading pthreads
source share
2 answers

the threads operate mostly independently, which means that each thread can use a signal handler to catch the "alarm" signal without killing the other threads. Therefore, you must add signal handlers.

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

A signal can be generated (and therefore expected) for the whole process (for example, when sent using kill (2)) or for a specific thread (for example, certain signals, such as SIGSEGV and SIGFPE, generated as a result of execution of a specific machine instruction are: threads, as well as signals aimed at a specific thread using pthread_kill (3)). The signal directed to the process can be delivered to any one of the streams that does not currently block the signal. If more than one of the streams has an unlocked signal, the Core selects an arbitrary stream to which the signal is transmitted.

+2
source share

The process itself will be segfault in your case.

If you need to assign NULL to a , you will see that it crashes in all probability. In the current code, you call a non-deterministic way. Some random location refers to a . Consequently, the behavior is undefined. Sometimes you will see a log statement in main , otherwise the program will crash. Consider yourself happy if the program crashes with such executions

If the thread executes a de-reference NULL pointer, it will be omitted. Its a process malfunction, not a stream wreck.

+5
source share

All Articles