Advanced Programming in UNIX Environment Episode 59

本文深入探讨了互斥锁、读写锁、条件变量和屏障的同步属性。详细解析了互斥锁属性的设置与查询,包括进程共享、健壮性和类型等关键属性,以及Linux和Solaris平台对健壮性属性的支持情况。
Synchronization Attributes

In this section, we discuss the attributes of mutexes, reader–writer locks, condition variables, and barriers.

Mutex Attributes

Mutex attributes are represented by a pthread_mutexattr_t structure. Whenever we initialized a mutex in Chapter 11, we accepted the default attributes by using the PTHREAD_MUTEX_INITIALIZER constant or by calling the pthread_mutex_init function with a null pointer for the argument that points to the mutex attribute structure.

#include <pthread.h>
int pthread_mutexattr_init(pthread_mutexattr_t *attr);
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);

We can use the pthread_mutexattr_getpshared function to query a pthread_mutexattr_t structure for its process-shared attribute. We can change the process-shared attribute with the pthread_mutexattr_setpshared function.

#include <pthread.h>

int pthread_mutexattr_getpshared(const pthread_mutexattr_t *, restrict attr, int *restrict pshared);
int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);

We can use the pthread_mutexattr_getrobust function to get the value of the robust mutex attribute. To set the value of the robust mutex attribute, we can call the pthread_mutexattr_setrobust function.

#include <pthread.h>

int pthread_mutexattr_getrobust(const pthread_mutexattr_t *restrict attr, int *restrict robust);
int pthread_mutexattr_setrobutst(pthread_mutexattr_t *attr, int robust);

There are two possible values for the robust attribute. The default is PTHREAD_MUTEX_STALLED, which means that no special action is taken when a process terminates while holding a mutex. In this case, use of the mutex can result in undefined behavior, and applications waiting for it to be unlocked are effectively ‘‘stalled.’’ The other value is PTHREAD_MUTEX_ROBUST. This value will cause a thread blocked in a call to pthread_mutex_lock to acquire the lock when another process holding the lock terminates without first unlocking it, but the return value from pthread_mutex_lock is EOWNERDEAD instead of 0.

Of the four platforms covered in this text, only Linux 3.2.0 currently supports robust pthread mutexes. Solaris 10 supports robust mutexes only in its Solaris threads library (see the mutex_init(3C) Solaris manual page for more information). However, in Solaris 11, robust pthread mutexes are supported.

#include <pthread.h>

int pthread_mutex_consistent(pthread_mutex_t *mutex);

PTHREAD_MUTEX_NORMAL

A standard mutex type that doesn’t do any special error checking or deadlock detection.

PTHREAD_MUTEX_ERRORCHECK

Amutex type that provides error checking.

PTHREAD_MUTEX_RECURSIVE

A mutex type that allows the same thread to lock it multiple times without first unlocking it. A recursive mutex maintains a lock count and isn’t released until it is unlocked the same number of times it is locked. Thus, if you lock a recursive mutex twice and then unlock it, the mutex remains locked until it is unlocked a second time.
PTHREAD_MUTEX_DEFAULT

A mutex type providing default characteristics and behavior. Implementations are free to map it to one of the other mutex types. For example, Linux 3.2.0 maps this type to the normal mutex type, whereas FreeBSD 8.0 maps it to the errorchecking type.

#include <pthread.h>

int pthread_mutexattr_gettype(const pthread_mutexattr * restrict attr, int *restrict type);
int pthread_mutexattr_settype(pthread_mutexattr_t *attr int type);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值