* Please refer to this document for reference from blog.csdn.net/wtz1985
The so-called "Lock" is to allow yourself to occupy space alone and prevent others from occupying their own resources before they are used up. the current operating system, whether it is windows, UNIX, and other operating systems. all adopt multi-threaded environments. this greatly increases the execution speed of tasks and does not affect the execution of other transactions. however, their execution depends on the rotation of time slices. If a thread is not finished, its time slice is used up and will be mounted until it is its next time slice. if we keep them so free and execute commands without constraints, it will lead to an unpredictable result. How can we solve this problem? See the following:
The reason for the lock is that a thread occupies its own resources when operating a transaction, and makes itself deduplicated. Before unlocking the lock, others can only wait, this effectively controls the way threads do not get unimaginable results due to time slice. the following code describes how the lock is implemented:
1. Interface for defining locks.
- /* ----- Locker. h -------*/
- # Ifndef _ locker_h
- # DEFINE _ locker_h
- Struct _ locker;
- Typedef struct _ locker;
- Typedef int (* lockerlockfunc) (locker * thiz );
- Typedef int (* lockerunlockfunc) (locker * thiz );
- Typedef void (* lockerdestroyfunc) (locker * thiz );
- Struct _ locker
- {
- Lockerlockfunc lock;
- Lockerunlockfunc unlock;
- Lockerdestroyfunc locker_destroy;
- Char priv [0];
- };
- Static inline int locker_lock (locker * thiz)
- {
- Assert (thiz! = NULL & thiz-> lock! = NULL );
- Return thiz-> lock;
- }
- Static inline int locker_unlock (locker * thiz)
- {
- Assert (thiz! = NULL & thiz-> unlock! = NULL );
- Return thiz-> unlock;
- }
- Static inline void locker_destroy (locker * thiz)
- {
- Assert (thiz! = NULL & thiz-> locker_destroy! = NULL );
- Return thiz-> destroy_locker;
- }
- # Endif/* _ locker_h */
2. Interface for defining thread locks.
- /* ------ Pthread_locker.h --------*/
- # Ifndef _ pthread_locker_h
- # DEFINE _ pthread_locker_h
- # Include "locker. H"
- Locker * locker_create (const char * Name );
- # Endif/* _ pthread_locker_h */
3. Interface implementation.
- /* ----- Pthread_locker.c ------*/
- # Include "pthread_locker.h"
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include <string. h>
- # Include <assert. h>
- Typedef struct _ privinfo
- {
- Char * str_name;
- Pthread_mutex_t mutex;
- } Privinfo;
- Static pthread_locker_lock (locker * thiz)
- {
- Assert (thiz! = NULL );
- Privinfo * priv = (privinfo *) thiz-> priv;
- Return pthread_mutex_lock (& (priv-> mutex ));
- }
- Static pthread_locker_unlock (locker * thiz)
- {
- Assert (thiz! = NULL );
- Privinfo * priv = (privinfo *) thiz-> priv;
- Return pthread_mutex_unlock (& (priv-> mutex ));
- }
- Static pthread_locker_destroy (locker * thiz)
- {
- Assert (thiz! = NULL );
- Privinfo * priv = (privinfo *) thiz-> priv;
- Free (priv-> Str );
- Pthread_mutex_destroy (& (priv-> mutex ));
- Free (thiz );
- Return;
- }
- Locker * locker_create (const char * name)
- {
- Locker * thiz = (locker *) malloc (sizeof (locker) + sizeof (privinfo ));
- Privinfo * priv = thiz-> priv;
- Priv-> str_name = strdup (name );
- Pthread_mutex_init (& (priv-> mutex), null );
- Thiz-> lock = pthread_locker_lock;
- Thiz-> unlock = pthread_lokcer_unlock;
- Thiz-> locker_destroy = pthread_locker_destroy;
- Return thiz;
- }
This is a simple implementation of the thread lock. But when you insert data in the linked list, you need to perform the search operation. This is only a simple thread lock that cannot be solved. How can this problem be solved? This involves the issue of recursive locks. The next article describes the implementation of recursive locks.