UNIX Environment Advanced Programming: Mutex properties

Source: Internet
Author: User
Tags error code inheritance modify mutex thread

The mutex has some properties that can be modified to control some of the behavior of the lock. The default mutex properties and their values are as follows:

Pshared:pthread_process_private

Type:pthread_mutex_default

Protocol:pthread_prio_none

prioceiling:–

Robustness:pthread_mutex_stalled_np

1. Get/Modify Shared Mutex attribute

pthread_mutexattr_t attr;  
int Pthread_mutexattr_init (pthread_mutexattr_t *attr);  
int Pthread_mutexattr_destroy (pthread_mutexattr_t *attr);  
int pthread_mutexattr_getpshared (pthread_mutexattr_t *attr,int *pshared);  
int pthread_mutexattr_setpshared (pthread_mutexattr_t *attr,int pshared);

Parameter: pshared value can be: pthread_process_shared,pthread_process_private

Description: 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, then only the thread that created the mutex in the same process can access the mutex.

2. Get/modify Type Mutex attribute

int Pthread_mutexattr_settype (pthread_mutexattr_t *attr,int kind);  
int Pthread_mutexattr_gettype (pthread_mutexattr_t *attr,int *kind);

Pthread_mutex_default (default mutex type attribute): This type of mutex does not automatically detect the deadlock. If a thread attempts to repeat 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 been unlocked, it can also cause unpredictable results. The POSIX standard stipulates that for a specific implementation, this type of mutex can be defined as another type of mutex.

Pthread_mutex_normal: This type of mutex does not automatically detect deadlocks. If a thread attempts to repeat 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 been unlocked, it can also cause unpredictable results.

Pthread_mutex_errorcheck: This type of mutex will automatically detect 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 has been unlocked, it will return an error code.

Pthread_mutex_recursive: If a thread repeatedly locks this type of mutex, it will not cause a deadlock. Multiple locks on a single thread for such mutexes must be repeated by this thread with the same number of unlocks, so that the mutex can be unlocked and other threads can get the mutex. Attempting to unlock a mutex locked by another thread will return an error code.

3. Set/Acquire the protocol properties of the mutex

int Pthread_mutexattr_setprotocol (pthread_mutexattr_t *attr, int protocol);  
int Pthread_mutexattr_getprotocol (const pthread_mutexattr_t *attr, int *protocol);

Possible values and implications of mutex protocol properties:

Pthread_prio_none: Thread precedence and scheduling are not affected by mutex ownership.

Pthread_prio_inherit: Low priority threads run at the priority level of a high-priority thread when high priority threads wait for the lower-priority thread to lock the mutex. This method will be passed in the form of inheritance. When a line threads unlocked a mutex, the priority of the thread is automatically taken to its original priority level. ("Priority inheritance" means that when one thread waits on a mutex owned by another low-priority thread, the priority of the latter is increased to the priority of the waiting thread.)

Pthread_prio_protect: A thread that owns a mutex of this type will run at its own priority and a thread of its own mutex to the higher priority of its own priority and the mutex it has. Other lock threads waiting for the thread have no effect on the scheduling priority of the thread.

Note: Pthread_prio_inherit and Pthread_prio_protect are available only in priority processes that adopt real-time scheduling policies Sched_fifo or SCHED_RR.

A thread can have multiple mutexes that are simultaneously initialized with the Pthread_prio_inherit and Pthread_prio_protect protocol properties. In this case, the thread is executed with the highest priority obtained through any of the protocols. Pthread_mutexattr_getprotocol can be used to obtain the protocol properties of the mutex Property object.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/unix/

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.