Could you please show me the way how I can print the parent first using a mutex…Thanks
void *print_message_function( void *ptr );
main() {
pthread_t thread1;
char *message1 = "Thread 1";
int iret1;
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
pthread_join( thread1, NULL);
printf("(Parent)Thread 1 returns: %d\n",iret1);
exit(0);
}
void *print_message_function( void *ptr ) {
char *message;
message = (char *) ptr;
printf("(Child)%s \n", message);
}
By acquiring a lock before creating a thread and then immediately trying to acquire that lock in your new thread you can guarantee that some code after the thread is created will execute before the thread can possibly have acquired the lock. Of course it would be simpler just to run the code before creating the thread in the first place.