posix 条件变量

 条件变量是一中线程同步机制,需要和 pthread_mutex_t配合使用才能完成任务,典型的可以应用在“生产者、消费者”模型中。

条件变量的数据类型:pthread_cond_t

配合的函数有:

1、pthread_cond_t结构的初始化、销毁函数

#include <pthread.h>
 
int pthread_cond_init(pthread_cond_t *restrict cond,                 pthread_condattr_t *restrict                                                                                           attr);
 
int pthread_cond_destroy(pthread_cond_t *cond);

 

Both return: 0 if OK, error number on failure

  2、获取条件变量的函数

#include <pthread.h>
 
int pthread_cond_wait(pthread_cond_t *restrict cond,
                      pthread_mutex_t *restrict      mutex);
 
int pthread_cond_timedwait(pthread_cond_t      *restrict cond,                           pthread_mutex_t      *restrict mutex, const struct timespec      *restrict timeout);

 

Both return: 0 if OK, error number on failure

 3、唤醒在条件变量上阻塞的函

#include <pthread.h>
 
int pthread_cond_signal(pthread_cond_t *cond);
 
int pthread_cond_broadcast(pthread_cond_t *cond);

 

Both return: 0 if OK, error number on failure

举例:

如果我们有一个不限制长度的队列,有多个线程向队列写入数据,有多个线程从队列读取数据。要求:1 同时只有一个线程操作对联。2如果队列没有数据,读取线程需要等待。3 写队列线程,写入数据后通知等待数据的线程。

我们可以用下面的代码实现:

队列使用的要点:

1、必须要用pthread_mutex_t保护队列

2、用条件变量实现,等待、通知的功能

3、条件等待的判断条件必须是while,防止spurious awaken.如get_msg函数中的红色部分。

 

在代码中,用蓝色注释,解释条件变量使用,发生的事情。

struct data_block * get_msg(struct message_queue *mq)

{

    struct data_block *db = NULL;

    pthread_mutex_lock(&mq->lock);//获取mq->lock锁  ,保护下面队列为空的判断 

    while(mb_empty(&mq->message_block))

         pthread_cond_wait(&mq->cond , &mq->lock);//释放mq->lock锁,获取mq->cond条件,线程阻塞。当写队列的线程发送signal信号的时候,获取mq->lock锁,释放mq->cond。  

    db = mb_pop_front(&mq->message_block);

 

    pthread_mutex_unlock(&mq->lock);//最后释放mq->lock锁

    return db;

}

 

int put_msg(struct message_queue *mq , struct data_block *db)

{

    pthread_mutex_lock(&mq->lock); //获取mq->lock锁  ,保护下面队列操作  

   int errn = mb_push_back(db , &mq->message_block);    pthread_metex_unlock(&mq->lock); //最后释放mq->lock锁

 pthread_cond_signal(&mq->cond);//通知阻塞在mq->cond的线程

    return errn;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值