Advanced Programming in UNIX Environment Episode 59
发布日期:2021-10-07 23:47:38 浏览次数:2 分类:技术文章

本文共 3386 字,大约阅读时间需要 11 分钟。

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 
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 
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 
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 
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 
int pthread_mutexattr_gettype(const pthread_mutexattr * restrict attr, int *restrict type);int pthread_mutexattr_settype(pthread_mutexattr_t *attr int type);

转载地址:https://blog.csdn.net/myfather103/article/details/79967354 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Advanced Programming in UNIX Environment Episode 60
下一篇:Advanced Programming in UNIX Environment Episode 58

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月08日 10时56分55秒