1

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;
} 
5
  • 1
    Use a pair of semaphores (or, if your religion forbids semaphores, use condition variables). Commented Dec 10, 2018 at 20:38
  • HI , i am beginner i don't know what do you mean by semaphores do you mean mutex lock and unlock ,because i want to do it with mutex only. Commented Dec 10, 2018 at 20:49
  • Is there a particular reason you insist on using an inadequate tool for this task? Commented Dec 10, 2018 at 20:51
  • yes , because i am student and the question in the exercice is to do it only that way . Commented Dec 10, 2018 at 20:54
  • Perhaps you should add some indentation, to make it easier to read your code? Commented Dec 10, 2018 at 21:51

1 Answer 1

2

Mutexes provide MUTual EXclusion, not ordering. You need to add something more or different to control relative order of operations between threads. For that purpose, the usual companions to mutexes are condition variables. You could do your job with one mutex, one condition variable, and one regular shared variable. Alternatively, a pair of semaphores could handle your particular job nicely and cleanly.

If the only synchronization objects you may use are mutexes, then you can try the mutex / CV approach without the CVs. The key here, with or without the CV, is to have a shared variable that somehow indicates which thread's turn it is. Each thread attempts to lock the mutex. On success, a thread checks the shared variable to see whether it is that thread's turn to run, and if so, it does the appropriate work, then releases the mutex. If a thread locks the mutex and discovers that it is not its turn, then it release the mutex and loops back to try again. The problem here is that it is possible for a thread to go an indefinite amount of time without being scheduled, and that is what adding a condition variable into the mix addresses.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.