Two threads, two mutexes, how can a dead loop be formed?

Two threads, two mutexes, how can a dead loop be formed?

[[351971]]

Fans’ questions must be arranged.

How can two threads and two mutexes deadlock?

The program flow chart is as follows:

Program flow chart

As shown in the figure above:

  1. At time t0, the main thread creates a child thread and initializes the mutexes mutex1 and mutex2;
  2. At time t1, the main thread applies for mutex1 and the child thread applies for mutex2;
  3. At time t2, both the main thread and the child thread sleep for 1 second to prevent the thread that gets the time slice first from directly applying for another mutex, causing the program to exit directly;
  4. At t3, both the main thread and the child thread want to acquire the mutex lock held by the other party, but neither party has time to release the lock in their own hand;
  5. At t4, both the main thread and the sub-thread go into sleep.

[Note] To ensure that the main thread and child thread can obtain the locks mutex1 and mutex2 respectively, each thread must sleep for 1 second after obtaining the lock. Otherwise, after the child thread is created, the main thread still has a certain time slice, and the main thread will apply for the lock mutex2, and a deadlock cannot be formed.

Deadlock

The source code is as follows

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <pthread.h>
  5.  
  6. unsigned int value1, value2, count ;
  7. pthread_mutex_t mutex1,mutex2;
  8. void * function (void *arg);
  9.  
  10.  
  11. void * function (void *arg)
  12. {
  13. pthread_mutex_lock(&mutex2);
  14. printf( "new thread get mutex2\n" );
  15. sleep(1);
  16. pthread_mutex_lock(&mutex1);
  17. printf( "new thread get mutex1\n" );
  18.   
  19.   
  20. pthread_mutex_unlock(&mutex1);
  21. printf( "new thread release mutex1\n" );
  22. pthread_mutex_unlock(&mutex2);
  23. printf( "new thread release mutex2\n" );
  24. return    NULL ;
  25. }
  26.  
  27. int main( int argc, char *argv[])
  28. {
  29. pthread_t a_thread;
  30.           
  31. if (pthread_mutex_init(&mutex1, NULL ) < 0)
  32. {
  33. perror( "fail to mutex_init" );
  34. exit(-1);
  35. }
  36. if (pthread_mutex_init(&mutex2, NULL ) < 0)
  37. {
  38. perror( "fail to mutex_init" );
  39. exit(-1);
  40. }
  41. if (pthread_create(&a_thread, NULL , function , NULL ) < 0)
  42. {
  43. perror( "fail to pthread_create" );
  44. exit(-1);
  45. }
  46. while ( 1 )
  47. {
  48. pthread_mutex_lock(&mutex1);
  49. printf( "main thread get mutex1\n" );
  50. sleep(1);
  51. pthread_mutex_lock(&mutex2);
  52. printf( "main thread get mutex2\n" );
  53. pthread_mutex_unlock(&mutex2);
  54. printf( "main thread release mutex2\n" );
  55. pthread_mutex_unlock(&mutex1);
  56. printf( "main thread release mutex1\n" );
  57. }
  58. return 0;
  59. }
  60. Compile and run

Compile and run

From the execution results, we can judge that the main thread and the child thread obtained the mutex locks mutex1 and mutex2 respectively. After sleeping for 1 second, they both wanted to apply for mutex2 and mutex1 respectively, but neither side wanted to release the locks in their hands. The locks have formed a deadlock, and the program has been in a dormant state.

Check the threads of the process

Check the process ID, which is 4204

Check the thread IDs created by the process: 4204, 4205.

This article is reprinted from the WeChat public account "Yikou Linux", which can be followed through the following QR code. To reprint this article, please contact Yikou Linux public account.

<<:  Foreign media: Global investment and deployment in 5G will accelerate in 2020

>>:  The full implementation of number portability is about to reach its first anniversary, and the winner may be different

Recommend

Why do we need RPC when we have HTTP?

This article briefly introduces the two forms of ...

The evolution of the Internet, the inevitability of blockchain

In the article "Bitcoin Prequel", it is...

In the era of data center explosion, attention should be paid to network cabling

With the advent of the big data era, all kinds of...

Latest version of Riverbed SteelCentral performance monitoring platform released

Riverbed Technology recently announced that the l...

China Mobile lost 8 million users. Why are users disappointed with China Mobile?

The introduction of the number portability policy...

The era of "small profits but quick turnover" for mobile data traffic is over

For four consecutive years, the government work r...

Huawei releases next-generation machine vision cameras and new products for 2020

[Hangzhou, China, May 25, 2020] Today, the Huawei...

Is there still room for wireless mesh networking in the enterprise?

【51CTO.com Quick Translation】Wireless mesh networ...