Implementation of recursive locks in multiple threads.

Source: Internet
Author: User
Tags assert

* Refer to this article please indicate from blog.csdn.net/wtz1985

In the previous article, I have explained the implementation of simple locks in multiple threads, but at the end of the day, I mentioned a problem, that is, if you are inserting in a linked list, the operation of the query, if only a simple lock, can not be achieved. So the "recursive lock" comes to the world.

Perhaps some people see recursion these two words, a bit silly eye, actually also nothing, simple introduction, is to carry on the simple count just. When the first reference to the lock, it produces it, when the lock is not untied, but also continue to use the lock, on the simple add one, untie one on the minus one, when the count is zero, the lock destroyed. The following is a simple procedure to explain how a recursive lock is implemented:

1, the definition of the recursive lock interface. (The definition of the interface of the lock has been defined in the previous article, it is not 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, the realization of the recursive lock. /*------ 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 ++; &NBSP;&NBSP}   else   {    locker_lock (priv->real_locker);      priv->lcoker_count = 1;     priv->current_pthread_locker = pthread_self ();   }   return 0; } STATIC&NBsp;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); &NBSP;&NBSP;&NBSP;&NBSP}     else     {           priv->locker_count --;    }  &nbsp}   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 the recursive lock. If you are familiar with COM, this recursive lock structure should be relatively simple, is a counter only. With this recursive lock, there's no need to worry. When the linked list is inserted, there is a problem with the query operation lock.

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.