how can i create two threads in C with pthead_create() but the first one prints 'h' and 'o' , and the second one prints 'ell' , and the result is 'hello' . how can we solve this problem using pthread_mutex_lock and unlock and without using any sleep(). Help please . this is what i did but sometimes it doesn't work as expected .
#include <stdio.h>
#include <pthread.h>
pthread_t th[2];
pthread_mutex_t l1,l2;
void *print1(){
pthread_mutex_lock( &l1 );
printf("h");
pthread_mutex_unlock( &l1 );
pthread_mutex_lock( &l2 );
printf("o");
pthread_mutex_unlock( &l2 );
return NULL;
}
void *print2(){
pthread_mutex_lock( &l2 );
printf("ell");
pthread_mutex_unlock( &l2 );
return NULL;
}
int main(){
pthread_create(&th[0],NULL,print1,NULL);
pthread_create(&th[1],NULL,print2,NULL);
pthread_join(th[0],NULL);
pthread_join(th[1],NULL);
printf("\n");
return 0;
}