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;
}
I need to run the main thread until the child threads finish the work. There is many ways to do this and as per me the simplest way is to use join. for ( int i = 0 ;i < childThreadList.Count; i++) { childThreadList [i].Join (); } ///...The following code will execute when all threads in the list have been terminated.../// Correct, my 5.
- CodeProject Multi Threading problem between Main and Child thread in C#. I have a main thread , 4 child thread work is assigned to all main and child threads, the problem is that main thread finishes the work fast, but child threads cannot.If main thread finishes fast before child thread the program hangs-on.
If you did not use it, the main thread (your application) may terminate while the child thread is still running. That is not a problem with your example but it will be when you have global or shared variables (child thread might access variables that does not exist anymore).
If thread::join () is NOT executed, then how does it can effect. That is what it happens when join () is called before return right? And when thread::join () is called before the while loop in main, main thread does not get a chance at all to execute the loop which is as expected.
The process itself will segfault in your case.
If you were to assign NULL to a
you can see that it will crash in all likelyhood. In the current code you invoke a
in non deterministic manner. Some random location is referenced by a
. Hence behavior is undefined. Sometimes you will see the log statement in main
, other times the program will crash. Consider yourself lucky if program crashes on such executions
If the thread does a NULL pointer de-reference, it will take the whole process down. Its a process crash and not a thread crash.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With