Two mutex, how to form an infinite loop?Fan questions must be arranged.

How can two threads, two mutex deadlock?

The program flow chart is as follows:

As shown above:

  1. At t0, the main thread creates a child thread and initializes mutex1 and mutex2.
  2. At t1, the main thread applied for mutex1 and the child thread applied for mutex2.
  3. At t2, both the main thread and the child thread sleep for 1 second to prevent the thread that obtains the time slice first from directly applying for another mutex, leading to the program exit directly.
  4. At t3, the main thread and the child thread both try to acquire the mutex from the other thread, but the other thread has no time to release its own lock.
  5. At t4, both the main thread and the subline go to sleep.

【 Note 】 In order to ensure that the main thread and child thread can obtain mutex1 and mutex2 locks respectively, they must sleep for 1 second after obtaining the lock, otherwise, the main thread will still have a certain time slice after creating the child thread, the main thread will apply for mutex2 lock, unable to form deadlock.

A deadlock

The source code is as follows

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include 



unsigned int value1, value2, count;

pthread_mutex_t  mutex1,mutex2;

void *function(void *arg);





void  *function(void *arg)

{

 pthread_mutex_lock(&mutex2);

 printf("new thread get mutex2\n"); 

 sleep(1);

 pthread_mutex_lock(&mutex1); 

 printf("new thread get mutex1\n"); 

 

 

 pthread_mutex_unlock(&mutex1);

 printf("new thread release mutex1\n");

 pthread_mutex_unlock(&mutex2); 

 printf("new thread release mutex2\n");

    return  NULL;

 }  



int main(int argc,  char *argv[])

{

 pthread_t  a_thread;

         

 if (pthread_mutex_init(&mutex1, NULL) < 0)

 {

  perror("fail to mutex_init");

  exit(- 1);

 }

  if (pthread_mutex_init(&mutex2, NULL) < 0)

 {

  perror("fail to mutex_init");

  exit(- 1);

 }              

 if (pthread_create(&a_thread, NULL, function, NULL) < 0)

 {   

  perror("fail to pthread_create");     

  exit(- 1);

 }

    while ( 1 )

    {

        pthread_mutex_lock(&mutex1);

  printf("main thread get mutex1\n");

  sleep(1);

        pthread_mutex_lock(&mutex2);  

  printf("main thread get mutex2\n");

        pthread_mutex_unlock(&mutex2);

  printf("main thread release mutex2\n");

        pthread_mutex_unlock(&mutex1);

  printf("main thread release mutex1\n");

    }

    return 0;

 }             

Copy the code

Compile operation

From the execution result, it can be seen that the main thread and the child thread have obtained mutex1 and mutex2 respectively, and after sleep 1 second, they both want to apply mutex2 and mutex1 respectively, but neither of them want to release the lock in their hands. The lock has formed a deadlock, and the program has been in hibernation.

Check the thread of the process

View the process ID, which is 4204Check the ids of the threads created by the process: 4204 and 4205.More embedded Linux knowledge please pay attention to the public number: a Linux