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)
- /* ------ Recursive_locker.h -------*/
- # Ifndef _ recursive_h
- # DEFINE _ recursive_h
- # Include "locker. H"
- Locker * recursive_locker_create (locker * real_locker );
- # Endif/* _ recursive_h */
2. Implement recursive locks.
- /* ------ Recursive_locker.c ------*/
- # Include "recursive_locker.h"
- # Include <stdlib. h>
- # Include <stdio. h>
- # Include <assert. h>
- # Include <string. h>
- Typedef struct _ privinfo
- {
- Locker * real_locker;
- Pthread_t current_pthread_locker;
- Int locker_count;
- } Privinfo;
- Static int recursive_locker_lock (locker * thiz)
- {
- Assert (thiz! = NULL );
- Privinfo * priv = thiz-> priv;
- If (priv-> current_pthread_locker = pthread_self ())
- {
- Priv-> locker_count ++;
- }
- Else
- {
- Locker_lock (priv-> real_locker );
- Priv-> lcoker_count = 1;
- Priv-> current_pthread_locker = pthread_self ();
- }
- Return 0;
- }
- Static int recursive_locker_unlock (locker * thiz)
- {
- Assert (thiz! = NULL );
- Privinfo * priv = thiz-> priv;
- If (priv-> current _> pthread_locker = pthread_self ())
- {
- If (priv-> locker_count = 1)
- {
- Priv-> locker_count = 0;
- Priv-> current_pthread_locker = 0;
- Locker_unlock (priv-> real_locker );
- }
- Else
- {
- Priv-> locker_count --;
- }
- }
- Else
- {
- Assert (! "Error ");
- }
- Return 0;
- }
- Static void recursive_locker_destroy (locker * thiz)
- {
- Assert (thiz! = NULL );
- Privinfo * priv = thiz-> priv;
- Locker_destroy (priv-> real_locker );
- Free (thiz );
- Return;
- }
- Locker * recursive_locker_create (locker * real_locker)
- {
- Locker * thiz = (locker *) malloc (sizeof (locker) + sizeof (privinfo ));
- Privinfo * priv = thiz-> priv;
- Priv-> real_locker = real_locker;
- Priv-> current_pthread_locker = 0;
- Priv-> locker_count = 0;
- Thiz-> lock = recursive_locker_lock;
- Thiz-> unlock = recursive_locker_unlock;
- Thiz-> locker_destroy = recursive_locker_destroy;
- Return thiz;
- }
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.