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

Blog    
Blog    

Recommend

The beauty of 5G phone is like being in your ear

[[352290]] This article is reprinted from the WeC...

Wireless sensor network standardization progress and protocol analysis

[[188829]] As an application-oriented research fi...

6 ways to remove duplicate URLs! (with detailed code)

[[341325]] This article is reprinted from the WeC...

How can you explain the communication protocol in such a simple way?

This article is reprinted from the WeChat public ...

3 ways to do next generation network migration correctly

They say that in travel, getting to your destinat...

The difference between NFV automation and NFV orchestration

NFV automation and NFV orchestration have overlap...

A brief discussion on the development history and future trends of routers

Labs Guide As a common network device, routers ca...

Edge Network Speed ​​Requirements

In the previous article, we mentioned how to surv...

IoT and 5G: Transforming Public Transportation Systems

Smart cities collect data from different connecte...

Exploring the evolution of Ethernet bandwidth for 5G bearer

From the voice services in the 2G era, to the ris...

The next technology that will change the world, 5G, is ready

Throughout history, every social transformation, ...