转载http://blog.csdn.net/relianceslee/article/details/952861同时请参见《unix 网络编程第二卷》
pthread_cond_wait( pthread_cond_t *cptr, pthread_mutex_t *mptr );
当mptr的值不是需要的值时,pthread_cond_wait函数等待之。
- 1.给条件变量发送信号的代码大体如下:
-
-
pthread_mutex_lock( &mutex );
-
设置条件为真
-
pthread_cond_signal( &cond );
-
pthread_mutex_unlock( &mutex );
-
-
2.测试条件并进入睡眠以等待该条件为真的代码大体如下:
-
-
pthread_mutex_lock( &mutex );
-
while( 条件为假 )
-
{
-
pthread_cond_wait( &cond, &mutex );
-
}
-
修改条件
-
pthread_mutex_unlock( &mutex );
-
-
3.为了上锁冲突,1的代码改为如下方式
-
-
pthread_mutex_lock( &mutex );
-
dosignal = ( nready.nready == 0 );
-
nready++;
-
pthread_mutex_unlock( &mutex );
-
-
if( dosignal )
-
{
-
pthread_cond_signal( &cond );
-
}
阅读(1617) | 评论(0) | 转发(0) |