Encapsulation of Locks in Linux

Source: Internet
Author: User
Tags function prototype mutex


Criticalsectionwrapper is an interface class

Class Criticalsectionwrapper {public:  //Factory method, constructor disabled  static criticalsectionwrapper* Createcriticalsection ();  Virtual ~criticalsectionwrapper () {}  //Tries to grab lock, beginning of a critical sections. Would wait for the  //lock to become available if the grab failed.  virtual void Enter () = 0;  Returns a grabbed lock, end of critical section.  virtual void Leave () = 0;};



criticalsectionwrapper* criticalsectionwrapper::createcriticalsection () {#ifdef _WIN32  return new Criticalsectionwindows (); #else  return new Criticalsectionposix (); #endif}




The Criticalsectionposix class inherits the Criticalsectionwrapper class and implements all the methods

Class Criticalsectionposix:public Criticalsectionwrapper {Public:criticalsectionposix ();  Virtual ~criticalsectionposix ();  virtual void Enter () OVERRIDE;  virtual void Leave () override;private:  pthread_mutex_t mutex_;};


Implementation of the Criticalsectionposix class

Criticalsectionposix::criticalsectionposix () {pthread_mutexattr_t attr;  (void) Pthread_mutexattr_init (&attr);  (void) Pthread_mutexattr_settype (&attr, pthread_mutex_recursive);  (void) Pthread_mutex_init (&mutex_, &attr);} Criticalsectionposix::~criticalsectionposix () {(void) Pthread_mutex_destroy (&mutex_);} Voidcriticalsectionposix::enter () {  (void) pthread_mutex_lock (&mutex_);} Voidcriticalsectionposix::leave () {  (void) pthread_mutex_unlock (&mutex_);}


Talk about the next few interfaces:

Thread and Thread synchronization objects (mutexes, read and write locks, condition variables) all have properties. The structure needs to be initialized before the property can be modified. The structure should be recycled after use. We use the Pthread_ mutexattr_init function to initialize the PTHREAD_MUTEXATTR structure and use the Pthread_mutexattr_destroy function to reclaim the structure.

/tr>

Name::

Pthread_mutexattr_init/pthread_mutexattr_destroy

Features:

Initialize/reclaim pthread_mutexattr_t structure
Header file:

#include <pthread.h>

Function:

int Pthread_mutexattrattr_init (pthread_mutexattr_ T *attr);

Int Pthread_mutexattrattr_destroy (pthread_mutexattr_t *attr);

Parameters:

attr    pthread_mutexattr_t structure Variable

Return value:

If successful returns 0, if failure returns an error number.






Pthread_mutexattr_init Initializes the value of the Property object to the default value . and allocates the memory space occupied by the Property object.

The Pshared property in attr represents the scope of the mutex created with this Property object, and its value can be pthread_process_private (the default value, which indicates that the mutex created by this Property object can only be used within the process) or Pthread_process_ SHARED.

The mutex attribute is divided into the shared mutex property and the type mutex property. The two properties are obtained by different functions and modified by different functions respectively. The pthread_mutexattr_getpshared and pthread_mutexattr_setpshared functions can obtain and modify shared mutex properties. The Pthread_mutexattr_gettype and Pthread_mutexattr_settype functions can obtain and modify type mutex properties. Here we introduce separately.

Header file:

Name::

pthread_mutexattr_getpshared/pthread_mutexattr_setpshared

Function:

Get/Modify Shared mutex properties

#include <pthread.h>

Function prototype:

int pthread_mutexattrattr_ getpshared (const pthread_attr_t *restrict attr, Int*restrict pshared);

Int pthread_mutexattrattr_ setpshared (  const pthread_attr_t *restrict attr,int pshared);

Parameters:

 

Return value:

If successful returns 0, if failure returns an error number.





The shared mutex property is used to specify the scope of the mutex. The domain of the mutex can be in-process or inter-process. Pthread_mutexattrattr_ getpshared can return a Property object's mutex scope property. Can be the following value: Pthread_process_shared,pthread_process_private. If the Pshared property of the Mutex Property object is placed Pthread_process_shared. The mutex created by this Property object is then saved in shared memory and can be shared by threads in multiple processes. If the Pshared property is set to Pthread_process_private, only threads in the same process as the thread that created the mutex can access the mutex.

Name::

Pthread_mutexattr_gettype/pthread_mutexattr_settype

Features:

Get/modify type Mutex properties

Header file:

#include <pthread.h>

Letter Number of prototypes:

int pthread_mutexattrattr_ getpshared (const pthread_attr_t *restrict attr,int*rest Rict pshared);

Int pthread_mutexattrattr_ setpshared (  const pthread_attr_t *restrict attr,int pshared);

Parameters:

 

Return value:

If successful returns 0, if failure returns an error number.

The Pthread_mutexattr_gettype function can obtain the mutex Type property. The default mutex Type property is Pthread_mutex_default.

The valid type attribute values are:

Pthread_mutex_normal;

Pthread_mutex_errorcheck;

pthread_mutex_recursive;

Pthread_mutex_default.

Type description:

Pthread_mutex_normal

This type of mutex does not automatically detect deadlocks. If a thread attempts to duplicate a lock on a mutex, it will cause the thread to deadlock. Attempting to unlock a mutex locked by another thread can cause unpredictable results. If a thread attempts to unlock a mutex that has already been unlocked, it will also throw unpredictable results.

Pthread_mutex_errorcheck

This type of mutex automatically detects deadlocks. If a thread attempts to repeat a lock on a mutex, an error code is returned. Attempting to unlock a mutex locked by another thread will return an error code. If a thread attempts to unlock a mutex that is already unlocked, it will return an error code.

Pthread_mutex_recursive

If a thread repeats a lock on this type of mutex, it does not cause a deadlock, and a thread repeats the same number of locks on such a mutex repeatedly, so that the mutex can be unlocked and the other thread can get the mutex. Attempting to unlock a mutex locked by another thread will return an error code. If a thread attempts to unlock a mutex that is already unlocked, it will return an error code. This type of mutex can only be process-private (the scope property is Pthread_process_private).

Pthread_mutex_default

This type of mutex does not automatically detect deadlocks. If a thread attempts to duplicate a lock on a mutex, it can cause unpredictable results. Attempting to unlock a mutex locked by another thread can cause unpredictable results. If a thread attempts to unlock a mutex that has already been unlocked, it will also throw unpredictable results. The POSIX standard stipulates that for a specific implementation, this type of mutex can be defined as another type of mutex.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Encapsulation of Locks in Linux

Related Article

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.