Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1670263
  • 博文数量: 695
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4027
  • 用 户 组: 普通用户
  • 注册时间: 2013-11-20 21:22
文章分类

全部博文(695)

文章存档

2018年(18)

2017年(74)

2016年(170)

2015年(102)

2014年(276)

2013年(55)

分类: C/C++

2014-06-11 20:21:31

int pthread_cond_wait
(
   pthread_cond_t * restrict cond,
   pthread_mutex_t *restrict mutex
);
这里参数cond是条件变量并且mutex是一个互斥的锁变量,这个锁必须提前被线程锁住.当这个函数被执行,调用的线程被强迫释放唯一的锁,释放了之 后,线程被挂起直到信号被唤醒.换醒线程的函数是pthread_cond_signal(pthread_cond_t *cond);唤醒等待该条件的某个线程.pthread_cond_broadcast(pthread_cond_t *cond);唤醒等待该条件的所有线程.

如果现成接受一个信号,并且被恢复,被重新将唯一的互斥锁锁住.这个函数将不返回直到正在调用的线程已经获得那个锁.

Where the parameter cond is the condition variable and mutex is the mutex previously locked by the calling thread.

When this function executes, the calling thread is forced ( internally ) to release the exclusive lock on the mutex. After the mutex is released, the thread is suspended until signaled. If the thread receives a signal and is resumed it will immediately attempt to re-establish the exclusive lock on the mutex. This function will not return until the calling thread the thread has gained that lock.

A couple of warnings:

* You should NEVER have multiple calls to pthread_cond_wait( ) or it's timed version with the same condition variable and different mutexes.

* You should avoid mixing and matching condition variables and mutexes which have different Process-Shared attributes e.g. one process private and the other process shared.

* You should always check the condition after a thread has returned from a wait. The reason for this is that systems can sometimes cause multiple threads to be unblocked even when the pthread_cond_signal function ( unblock one thread ) is used.

Since a call to pthread_cond_wait( ) suspends the calling thread until it receives a signal the potential exists for the caller to be suspended forever. If you want to place an upper limit on the amount of time the thread will suspend you should use pthread_cond_timewait( ) described below.

 


阅读(617) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~