If I am getting these results, first of all I would do the following:
1) Instead of below lines,
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
replace it with,
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
pthread_join( thread1, NULL);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
pthread_join( thread2, NULL);
and see what is the result.
2) Inside your thread function, you need to call pthread_exit("Exit"); This is a proper way to exit from the thread function. Do it at the end of function.
void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
pthread_exit("Exit");
}
If you are doing it this way, ideally you should not face any problem. In every case, i am assuming you are compiling your program using gcc -D_REENTRANT -o threadex threadex.c -lpthread
This is not the final solution. If it is going well, then we can proceed to next step of starting both the threads at a time.
Please share the feedback after incorporating these changes.