Multi-thread lock implementation.

Source: Internet
Author: User

* 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.

  1. /* ----- Locker. h -------*/
  2. # Ifndef _ locker_h
  3. # DEFINE _ locker_h
  4. Struct _ locker;
  5. Typedef struct _ locker;
  6. Typedef int (* lockerlockfunc) (locker * thiz );
  7. Typedef int (* lockerunlockfunc) (locker * thiz );
  8. Typedef void (* lockerdestroyfunc) (locker * thiz );
  9. Struct _ locker
  10. {
  11. Lockerlockfunc lock;
  12. Lockerunlockfunc unlock;
  13. Lockerdestroyfunc locker_destroy;
  14. Char priv [0];
  15. };
  16. Static inline int locker_lock (locker * thiz)
  17. {
  18. Assert (thiz! = NULL & thiz-> lock! = NULL );
  19. Return thiz-> lock;
  20. }
  21. Static inline int locker_unlock (locker * thiz)
  22. {
  23. Assert (thiz! = NULL & thiz-> unlock! = NULL );
  24. Return thiz-> unlock;
  25. }
  26. Static inline void locker_destroy (locker * thiz)
  27. {
  28. Assert (thiz! = NULL & thiz-> locker_destroy! = NULL );
  29. Return thiz-> destroy_locker;
  30. }
  31. # Endif/* _ locker_h */

2. Interface for defining thread locks.

  1. /* ------ Pthread_locker.h --------*/
  2. # Ifndef _ pthread_locker_h
  3. # DEFINE _ pthread_locker_h
  4. # Include "locker. H"
  5. Locker * locker_create (const char * Name );
  6. # Endif/* _ pthread_locker_h */

3. Interface implementation.

  1. /* ----- Pthread_locker.c ------*/
  2. # Include "pthread_locker.h"
  3. # Include <stdio. h>
  4. # Include <stdlib. h>
  5. # Include <string. h>
  6. # Include <assert. h>
  7. Typedef struct _ privinfo
  8. {
  9. Char * str_name;
  10. Pthread_mutex_t mutex;
  11. } Privinfo;
  12. Static pthread_locker_lock (locker * thiz)
  13. {
  14. Assert (thiz! = NULL );
  15. Privinfo * priv = (privinfo *) thiz-> priv;
  16. Return pthread_mutex_lock (& (priv-> mutex ));
  17. }
  18. Static pthread_locker_unlock (locker * thiz)
  19. {
  20. Assert (thiz! = NULL );
  21. Privinfo * priv = (privinfo *) thiz-> priv;
  22. Return pthread_mutex_unlock (& (priv-> mutex ));
  23. }
  24. Static pthread_locker_destroy (locker * thiz)
  25. {
  26. Assert (thiz! = NULL );
  27. Privinfo * priv = (privinfo *) thiz-> priv;
  28. Free (priv-> Str );
  29. Pthread_mutex_destroy (& (priv-> mutex ));
  30. Free (thiz );
  31. Return;
  32. }
  33. Locker * locker_create (const char * name)
  34. {
  35. Locker * thiz = (locker *) malloc (sizeof (locker) + sizeof (privinfo ));
  36. Privinfo * priv = thiz-> priv;
  37. Priv-> str_name = strdup (name );
  38. Pthread_mutex_init (& (priv-> mutex), null );
  39. Thiz-> lock = pthread_locker_lock;
  40. Thiz-> unlock = pthread_lokcer_unlock;
  41. Thiz-> locker_destroy = pthread_locker_destroy;
  42. Return thiz;
  43. }

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.

 

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.