Reputation: 127
I have to execute this code manually and figure the output, I tried it out and could not find out what am missing, here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *thread_function(void *arg) {
printf("Hello World :) \n");
exit(EXIT_SUCCESS);
}
int main(void) {
pthread_t mythread;
if(pthread_create(&mythread, NULL, thread_function, NULL)) {
fprintf(stderr, "Failure 1?\n");
exit(EXIT_FAILURE);
}
printf("I have to wait ? \n");
if (pthread_join(mythread, NULL)){
fprintf(stderr, "Failure 2 ?");
exit(EXIT_FAILURE);
}
printf("Goodbye Cruel World :(\n");
return 0;
}
The expected output is:
I have to wait ?
Hello World :)
Goodbye Cruel World :(
The output I got:
I have to wait ?
Hello World :)
Why the code skipped the last print?
Upvotes: 1
Views: 75
Reputation: 182761
The call to exit
in the thread will terminate the process before it has a chance to output the "Goodbye" message. If you want to terminate a thread, use pthread_exit
, not exit
.
Upvotes: 2