Thread Synchronization-recursive lock non-recursive lock

Source: Internet
Author: User

I. Introduction

1.1 process/Thread Synchronization Method

Common process/Thread Synchronization Methods include Mutex, rdlock, cond, and Semophore.

In windows, the Critical Section and Event are also common Synchronization Methods.

1.2 recursive lock/non-recursive lock

Mutex can be divided into recursive mutex and non-recursive mutex ). Recursive locks are also called reentrant mutex. non-recursive locks are also called non-reentrant mutex ).

UniqueDifferencesYes:

The same thread can obtain the same recursive lock Multiple times without deadlock.

If a thread acquires the same non-recursive lock multiple times, a deadlock occurs.


The Mutex and Critical sections in Windows are recursive.

The pthread_mutex_t lock in Linux is non-recursive by default. You can set the PTHREAD_MUTEX_RECURSIVE attribute to set the pthread_mutex_t lock to a recursive lock.

Ii. Code

2.1 Critical Section recursive lock

[Cpp]View plaincopy
  1. # Include # Include
  2. # Include
  3. Int counter = 0;
  4. CRITICAL_SECTION g_cs;
  5. Void doit (void * arg ){
  6. Int I, val; for (I = 0; I <5000; I ++)
  7. {EnterCriticalSection (& g_cs );
  8. EnterCriticalSection (& g_cs );
  9. Val = counter; printf ("thread % d: % d \ n", int (arg), val + 1 );
  10. Counter = val + 1;
  11. LeaveCriticalSection (& g_cs );
  12. }}
  13. Int main (int argc, char * argv [])
  14. {InitializeCriticalSection (& g_cs );
  15. HANDLE hThread1 = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) doit, (void *) 1, 0, NULL );
  16. HANDLE hTrehad2 = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) doit, (void *) 2, 0, NULL );
  17. WaitForSingleObject (hThread1, INFINITE); WaitForSingleObject (hTrehad2, INFINITE );
  18. DeleteCriticalSection (& g_cs );
  19. Return 0 ;}
    Result: The values of 1-2 locks and 1-2 locks can be correctly output ~ 10000.

    2.2 pthread_mutex_t non-recursive lock

    [Cpp]View plaincopy
    1. # Include # Include
    2. # Include
    3. Int counter = 0;
    4. Pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
    5. Void * doit (void *){
    6. Int I, val; for (I = 0; I <5000; I ++)
    7. {Pthread_mutex_lock (& g_mutex );
    8. Pthread_mutex_lock (& g_mutex );
    9. Val = counter; printf ("% x: % d \ n", pthread_self (), val + 1 );
    10. Counter = val + 1;
    11. Pthread_mutex_unlock (& g_mutex );
    12. }}
    13. Int main (int argc, char * argv [])
    14. {Pthread_t tid1, tid2;
    15. Pthread_create (& tid1, NULL, doit, NULL );
    16. Pthread_create (& tid2, NULL, doit, NULL );
    17. Pthread_join (tid1, NULL); pthread_join (tid2, NULL );
    18. Return 0;
    19. }

      Result: After the lock is applied, the result is correct ~ 10000; add two locks, deadlock, no output information.

      2.3 pthread_mutex_t recursive lock (PTHREAD_MUTEX_RECURSIVE)

      [Cpp]View plaincopy
      1. # Include # Include
      2. # Include
      3. Int counter = 0;
      4. Pthread_mutex_t g_mutex; // = PTHREAD_MUTEX_INITIALIZER;
      5. Void * doit (void *){
      6. Int I, val; for (I = 0; I <5000; I ++)
      7. {Pthread_mutex_lock (& g_mutex );
      8. Pthread_mutex_lock (& g_mutex );
      9. Val = counter; printf ("% x: % d \ n", pthread_self (), val + 1 );
      10. Counter = val + 1;
      11. Pthread_mutex_unlock (& g_mutex );
      12. }}
      13. Int main (int argc, char * argv [])
      14. {// Create recursive attribute
      15. Pthread_mutexattr_t attr; pthread_mutexattr_init (& attr );
      16. // Set recursive attribute
      17. Pthread_mutexattr_settype (& attr, PTHREAD_MUTEX_RECURSIVE );
      18. Pthread_mutex_init (& g_mutex, & attr );
      19. Pthread_t tid1, tid2;
      20. Pthread_create (& tid1, NULL, doit, NULL); pthread_create (& tid2, NULL, doit, NULL );
      21. Pthread_join (tid1, NULL );
      22. Pthread_join (tid2, NULL );
      23. Pthread_mutex_destroy (& g_mutex );
      24. // Destroy recursive attribute
      25. Pthread_mutexattr_destroy (& attr );
      26. Return 0 ;}

        Result: The values of 1-2 locks and 1-2 locks can be correctly output ~ 10000. ,

        In thread synchronization, using locks is a very common practice. Try to use non-recursive locks to avoid using recursive locks!

        The logic of non-recursive locks is clear and can be easily debugged when a deadlock occurs! You only need to use non-recursive locks with one click!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.