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

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

Reader–Writer Locking with Timeouts

Just as with mutexes, the Single UNIX Specification provides functions to lock

reader–writer locks with a timeout to give applications a way to avoid blocking
indefinitely while trying to acquire a reader–writer lock. These functions are
pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock.

#include 
#include
int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rwlock, const struct timespec *retsrict tsp);int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rwlock, const struct timespec *retsrict tsp);

Condition Variables

Condition variables are another synchronization mechanism available to threads. These synchronization objects provide a place for threads to rendezvous. When used with mutexes, condition variables allow threads to wait in a race-free way for arbitrary conditions to occur.

The condition itself is protected by a mutex. A thread must first lock the mutex to change the condition state. Other threads will not notice the change until they acquire the mutex, because the mutex must be locked to be able to evaluate the condition.

#include 
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);int pthread_cond_destroy(pthread_cond_t *cond);

We use pthread_cond_wait to wait for a condition to be true. A variant is provided to return an error code if the condition hasn’t been satisfied in the specified amount of time.

#include 
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);int pthread_cond_timewait(pthread_cond_t *restrict, pthread_mutex_t *restrict mutex, const struct timespce *restrict tsptr);

The mutex passed to pthread_cond_wait protects the condition. The caller passes it locked to the function, which then atomically places the calling thread on the list of threads waiting for the condition and unlocks the mutex.

There are two functions to notify threads that a condition has been satisfied. The pthread_cond_signal function will wake up at least one thread waiting on a condition, whereas the pthread_cond_broadcast function will wake up all threads waiting on a condition.

The POSIX specification allows for implementations of pthread_cond_signal to wake up more than one thread, to make the implementation simpler.

#include 
int pthread_cond_signal(pthread_cond_t *cond);int pthread_cond_broadcast(pthread_cond_t *cond);
#include 
struct msg{ struct msg *m_next;}struct msg *workq;pthread_cond_t qread=PTHREAD_COND_INITIALIZER;pthread_mutex_t qlock=PTHREAD_MUTEX_INITIALIZER;void process_msg(void){ struct msg *mp; for(;;) { pthread_mutex_lock(&qlock); while(workq==NULL) pthread_cond_wait(&qready,&qlock); mp=workq; workq=mp->m_next; pthread_mutex_unlock(&qlock); }}void enqueue_msg(struct msg *mp){ pthread_mutex_lock(&qlock); mp->m_next=workq; workq=mp; pthread_mutex_unlock(&qlock); pthread_cond_signal(&qready);}

Using a condition variable

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

上一篇:Advanced Programming in UNIX Environment Episode 57
下一篇:Advanced Programming in UNIX Environment Episode 55

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月05日 03时39分17秒