Implementation of recursive locks in multiple threads

Source: Internet
Author: User

This article Reprinted from: http://blog.csdn.net/wtz1985/article/details/301637

In the previous article, I have already elaborated on the Implementation of simple locks in multiple threads. At the end of the article, I raised the question that when inserting a chain table, the query operation cannot be implemented if it is just a simple lock. So the "recursive lock" emerged in the world.

Some people may see the word recursion, which is a bit silly. In fact, there is nothing. A simple introduction is just a simple counting. When the lock is referenced at the beginning, it will be generated. When the lock is not unlocked, we will continue to use the lock, simply add one, unlock one, then subtract one, when the count is zero, destroy the lock pin. The following describes how recursive locks are implemented using a program:

1. Recursive lock interface definition. (The definition of the lock interface has already been defined in the previous article. It will not be repeated here)

  1. /* ------ Recursive_locker.h -------*/
  2. # Ifndef _ recursive_h
  3. # DEFINE _ recursive_h
  4. # Include "locker. H"
  5. Locker * recursive_locker_create (locker * real_locker );
  6. # Endif/* _ recursive_h */

2. Implement recursive locks.

  1. /* ------ Recursive_locker.c ------*/
  2. # Include "recursive_locker.h"
  3. # Include <stdlib. h>
  4. # Include <stdio. h>
  5. # Include <assert. h>
  6. # Include <string. h>
  7. Typedef struct _ privinfo
  8. {
  9. Locker * real_locker;
  10. Pthread_t current_pthread_locker;
  11. Int locker_count;
  12. } Privinfo;
  13. Static int recursive_locker_lock (locker * thiz)
  14. {
  15. Assert (thiz! = NULL );
  16. Privinfo * priv = thiz-> priv;
  17. If (priv-> current_pthread_locker = pthread_self ())
  18. {
  19. Priv-> locker_count ++;
  20. }
  21. Else
  22. {
  23. Locker_lock (priv-> real_locker );
  24. Priv-> lcoker_count = 1;
  25. Priv-> current_pthread_locker = pthread_self ();
  26. }
  27. Return 0;
  28. }
  29. Static int recursive_locker_unlock (locker * thiz)
  30. {
  31. Assert (thiz! = NULL );
  32. Privinfo * priv = thiz-> priv;
  33. If (priv-> current _> pthread_locker = pthread_self ())
  34. {
  35. If (priv-> locker_count = 1)
  36. {
  37. Priv-> locker_count = 0;
  38. Priv-> current_pthread_locker = 0;
  39. Locker_unlock (priv-> real_locker );
  40. }
  41. Else
  42. {
  43. Priv-> locker_count --;
  44. }
  45. }
  46. Else
  47. {
  48. Assert (! "Error ");
  49. }
  50. Return 0;
  51. }
  52. Static void recursive_locker_destroy (locker * thiz)
  53. {
  54. Assert (thiz! = NULL );
  55. Privinfo * priv = thiz-> priv;
  56. Locker_destroy (priv-> real_locker );
  57. Free (thiz );
  58. Return;
  59. }
  60. Locker * recursive_locker_create (locker * real_locker)
  61. {
  62. Locker * thiz = (locker *) malloc (sizeof (locker) + sizeof (privinfo ));
  63. Privinfo * priv = thiz-> priv;
  64. Priv-> real_locker = real_locker;
  65. Priv-> current_pthread_locker = 0;
  66. Priv-> locker_count = 0;
  67. Thiz-> lock = recursive_locker_lock;
  68. Thiz-> unlock = recursive_locker_unlock;
  69. Thiz-> locker_destroy = recursive_locker_destroy;
  70. Return thiz;
  71. }

The above is the implementation of recursive locks. If you are familiar with COM, the recursive lock structure should be relatively simple, just a counter. With this recursive lock, you don't have to worry about the query operation lock when inserting the linked list.

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.