Can you explain why in Linux (not in Mac) I get Segmentation Fault when I do:
pthread_join(thread2, (void**)&status);
pthread_join(thread1, (void**)&status);
But is ok when I do:
pthread_join(thread1, (void**)&status);
pthread_join(thread2, (void**)&status);
I tried on Mac and everything is fine, but in Linux the code run properly only if I do the join of the thread1 and after that the join of thread2...
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *print_msg( char *ptr );
main(){
pthread_t thread1, thread2;
char *message1 = "Ping";
char *message2 = "Pong";
int status;
pthread_create(&thread1, NULL, print_msg, message1);
printf("tid thread1= %d\n", (int)thread1);
pthread_create(&thread2, NULL, print_msg, message2);
printf("tid thread2= %d\n", (int)thread2);
pthread_join(thread2, (void**)&status);
pthread_join(thread1, (void**)&status);
printf("Thread 1 end with: %d\n", (int)status);
printf("Thread 2 end with: %d\n", (int)status);
exit(0);
}
void *print_msg( char *ptr ){
char *msg;
void *val=0;
msg = (char *) ptr;
printf("%s \n", msg);
pthread_exit(val);
}