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

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

Reader–Writer Lock Attributes

Reader–writer locks also have attributes, similar to mutexes. We use pthread_rwlockattr_init to initialize a pthread_rwlockattr_t structure and pthread_rwlockattr_destroy to deinitialize the structure.

#include 
int pthread_rwlockattr_init(pthread_rwlockattr_t *attr);int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);

Just as with the mutex process-shared attributes, a pair of functions is provided to get and set the process-shared attributes of reader–writer locks.

#include 
int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t * restrict attr, int *restrict pshared);int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared);

Although POSIX defines only one reader–writer lock attribute, implementations are free to define additional, nonstandard ones.

Condition Variable Attributes

The Single UNIX Specification currently defines two attributes for condition variables: the process-shared attribute and the clock attribute. As with the other attribute objects, a pair of functions initialize and deinitialize condition variable attribute objects.

#include 
int pthread_condattr_init(pthread_condattr_t *attr);int pthread_condattr_destroy(pthread_condattr_t *attr);

To find the current value of the process-shared attribute, we use the pthread_condattr_getpshared function. To set its value, we use the pthread_condattr_setpshared function.

#include 
int pthread_condattr_getpshared(const pthread_condattr_t * restrict attr, int *restrict pshared);int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared);

We can use the pthread_condattr_getclock function to retrieve the clock ID that will be used by the pthread_cond_timedwait function for the condition variable that was initialized with the pthread_condattr_t object. We can change the clock ID with the pthread_condattr_setclock function.

#include 
int pthread_condattr_getclock(const pthread_condattr_t *restrict attr, clockid_t *restrict clock_id);int pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id);

Curiously, the Single UNIX Specification doesn’t define the clock attribute for any of the other attribute objects that have a wait function with a timeout.

Barrier Attributes

Barriers have attributes, too. We can use the pthread_barrierattr_init function to initialize a barrier attributes object and the pthread_barrierattr_destroy function to deinitialize a barrier attributes object.

#include 
int pthread_barrierattr_init(pthread_barrierattr_t *attr);int pthread_barrierattr_destroy(pthread_barrierattr_t *attr);

As with the other attribute objects, we have one function to get the attribute (pthread_barrierattr_getpshared) value and one function to set the value (pthread_barrierattr_setpshared).

#include 
int pthread_barrierattr_getpshared(const pthread_battierattr_t *restrict attr, int *restrict pshared);int pthread_barrierattr_setpshared(pthread_barriersttr_t *attr, int pshared);

The value of the process-shared attribute can be either PTHREAD_PROCESS_SHARED (accessible to threads from multiple processes) or PTHREAD_PROCESS_PRIVATE (accessible to only threads in the process that initialized the barrier).

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

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

发表评论

最新留言

不错!
[***.144.177.141]2024年04月20日 22时44分34秒