Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Two funny types of deadlock

case 1: two Mutex are used: the 1st mutex is applied, and second mutex fails, and because of the poor design, the 1st mutex is not unlocked, and this causes all other threads (including the thread which holds the 2nd mutex) to halt. Thus, the 2nd mutex is locked perpetually, and so does the 1st mutex.

case 2: two mutex are used; the order of applying the mutex matters.

   void *function1()
{
...
pthread_mutex_lock(&lock1); - Execution step 1
pthread_mutex_lock(&lock2); - Execution step 3 DEADLOCK!!!
...
...
pthread_mutex_lock(&lock2);
pthread_mutex_lock(&lock1);
...
}

void *function2()
{
...
pthread_mutex_lock(&lock2); - Execution step 2
pthread_mutex_lock(&lock1);
...
...
pthread_mutex_lock(&lock1);
pthread_mutex_lock(&lock2);
...
}

main()
{
...

pthread_create(&thread2, NULL, function1, NULL);
...
}



This post first appeared on Always Remember, You Are At Most Yourself; And, Yo, please read the originial post: here

Share the post

Two funny types of deadlock

×

Subscribe to Always Remember, You Are At Most Yourself; And, Yo

Get updates delivered right to your inbox!

Thank you for your subscription

×