线程条件变量详解

函数:

pthread_cond_init, pthread_cond_destroy, pthread_cond_signal,

pthread_cond_broadcast, pthread_cond_wait, pthread_cond_timedwait

概要:

#include <pthread.h>

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr);

int pthread_cond_signal(pthread_cond_t *cond);

int pthread_cond_broadcast(pthread_cond_t *cond);

int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);

int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime);

int pthread_cond_destroy(pthread_cond_t *cond);

 

描述:

A condition (short for ``condition variable'') is a synchronization

device that allows threads to suspend execution and relinquish the pro-

cessors until some predicate on shared data is satisfied. The basic

operations on conditions are: signal the condition (when the predicate

becomes true), and wait for the condition, suspending the thread execu-

tion until another thread signals the condition.


A condition variable must always be associated with a mutex, to avoid

the race condition where a thread prepares to wait on a condition vari-

able and another thread signals the condition just before the first

thread actually waits on it.


pthread_cond_init initializes the condition variable cond, using the

condition attributes specified in cond_attr, or default attributes if

cond_attr is NULL. The LinuxThreads implementation supports no

attributes for conditions, hence the cond_attr parameter is actually

ignored.


Variables of type pthread_cond_t can also be initialized statically,

using the constant PTHREAD_COND_INITIALIZER.

pthread_cond_signal restarts one of the threads that are waiting on the

condition variable cond. If no threads are waiting on cond, nothing

happens. If several threads are waiting on cond, exactly one is

restarted, but it is not specified which.


pthread_cond_broadcast restarts all the threads that are waiting on the

condition variable cond. Nothing happens if no threads are waiting on

cond.


pthread_cond_wait atomically unlocks the mutex (as per

pthread_unlock_mutex) and waits for the condition variable cond to be

signaled. The thread execution is suspended and does not consume any

CPU time until the condition variable is signaled. The mutex must be

locked by the calling thread on entrance to pthread_cond_wait. Before

returning to the calling thread, pthread_cond_wait re-acquires mutex

(as per pthread_lock_mutex).


Unlocking the mutex and suspending on the condition variable is done

atomically. Thus, if all threads always acquire the mutex before sig-

naling the condition, this guarantees that the condition cannot be sig-

naled (and thus ignored) between the time a thread locks the mutex and

the time it waits on the condition variable.

这段话说明之所以在调用pthread_cond_wait之前线程需要加锁,是因为从调用

 

pthread_cond_wait到进入condition wait的悬起状态之间的时间如果其他线程调用 pthread_cond_signal,这个signal将被忽略。为了不让这个signal被忽略,必须使调用 pthread_cond_wait到进入condition wait的悬起状态这段时间具有原子性,办法就是

在调用pthread_cond_wait和pthread_cond_signal之前都进行加锁,在线程进入

condition wait的悬起状态之前一刻释放锁


 

pthread_cond_timedwait atomically unlocks mutex and waits on cond, as

pthread_cond_wait does, but it also bounds the duration of the wait. If

cond has not been signaled within the amount of time specified by

abstime, the mutex mutex is re-acquired and pthread_cond_timedwait

returns the error ETIMEDOUT. The abstime parameter specifies an abso-

lute time, with the same origin as time(2) and gettimeofday(2); an abstime

of 0 corresponds to 00:00:00 GMT, January 1, 1970.


pthread_cond_destroy destroys a condition variable, freeing the

resources it might hold. No threads must be waiting on the condition

variable on entrance to pthread_cond_destroy. In the LinuxThreads

implementation, no resources are associated with condition variables,

thus pthread_cond_destroy actually does nothing except checking that

the condition has no waiting threads.

 

线程取消:

pthread_cond_wait and pthread_cond_timedwait are cancellation points.

If a thread is cancelled while suspended in one of these functions, the

thread immediately resumes execution, then locks again the mutex argu-

ment to pthread_cond_wait and pthread_cond_timedwait, and finally exe-

cutes the cancellation. Consequently, cleanup handlers are assured

that mutex is locked when they are called.

pthread_cond_wait 和pthread_cond_timedwait都是线程可取消点,可以被 pthread_cancel取消,为了避免在pthread_cond_wait和pthread_mutex_cancel之间 被取消,可以使用线程清理函数处理,在清理函数中解锁

异步安全性:

The condition functions are not async-signal safe, and should not be

called from a signal handler. In particular, calling pthread_cond_sig-

nal or pthread_cond_broadcast from a signal handler may deadlock the

calling thread.

 

条件变量不具备异步信号安全性,因此不要在信号处理函数中调用pthread_cond_signal 和pthread_cond_broadcast

 

返回值:

All condition variable functions return 0 on success and a non-zero

error code on error.

错误

 

pthread_cond_init, pthread_cond_signal, pthread_cond_broadcast, and

pthread_cond_wait never return an error code.


The pthread_cond_timedwait function returns the following error codes

on error:

ETIMEDOUT

the condition variable was not signaled until the timeout

specified by abstime


EINTR pthread_cond_timedwait was interrupted by a signal

The pthread_cond_destroy function returns the following error code on

error:

EBUSY some threads are currently waiting on cond.

例子:

Consider two shared variables x and y, protected by the mutex mut, and

a condition variable cond that is to be signaled whenever x becomes

greater than y.

为了避免其他因素激发condition wait状态的线程,可以使pthread_cond_wait 被激发仅仅成为必要条件,而while()中给出的条件才是我们等待的充分条件


 


Waiting until x is greater than y is performed as follows:


 


Modifications on x and y that may cause x to become greater than y

should signal the condition if needed:


 


If it can be proved that at most one waiting thread needs to be waken

up (for instance, if there are only two threads communicating through x

and y), pthread_cond_signal can be used as a slightly more efficient

alternative to pthread_cond_broadcast. In doubt, use

pthread_cond_broadcast.


To wait for x to becomes greater than y with a timeout of 5 seconds,

do:

























































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值