生产者消费者模型(C++)

/*  手撕生产者与消费者模型
 *  一个场所,两种角色,三种关系
 *  场所:线程 安全的队列
 */
#include <iostream>
#include <queue>
#include <pthread.h>
class BlockQueue
{
    public:
    BlockQueue(int cap = 10):_capacity(cap){
        pthread_mutex_init(&_mutex, NULL);
        pthread_cond_init(&_cond_productor, NULL);
        pthread_cond_init(&_cond_consumer, NULL);
    }
    ~BlockQueue(){
        pthread_mutex_destroy(&_mutex);
        pthread_cond_destroy(&_cond_productor);
        pthread_cond_destroy(&_cond_consumer);
    }
    bool QueuePush(int data) {
        QueueLock();        //加锁
        while (QueueIsFull()) { //队列满了
        ProductorWait();    //生产者休眠
        }
        _queue.push(data);
        ConsumerWakeUp();       //唤醒消费者
        QueueUnLock();      //解锁
        return true;
    }
    bool QueuePop(int *data) {
        QueueLock();        //加锁
        while (QueueIsEmpty()) { //队列空
        ConsumerWait();    //消费者休眠
        }
        *data = _queue.front();
        _queue.pop();
        ProductorWakeUp();      //唤醒生产者
        QueueUnLock();      //解锁
        return true;
    }
    private:
    void QueueLock() {
        pthread_mutex_lock(&_mutex);
    }
    void QueueUnLock() {
        pthread_mutex_unlock(&_mutex);
    }
    void ProductorWait(){
        pthread_cond_wait(&_cond_productor, &_mutex);
    }
    void ProductorWakeUp() {
        pthread_cond_signal(&_cond_productor);
    }
    void ConsumerWait(){
        pthread_cond_wait(&_cond_consumer, &_mutex);
    }
    void ConsumerWakeUp(){
        pthread_cond_signal(&_cond_consumer);
    }
    bool QueueIsFull(){
        return (_queue.size() == _capacity);
    }
    bool QueueIsEmpty(){
        return _queue.empty();
    }
    private:
    std::queue<int> _queue;
    int _capacity;
    pthread_mutex_t _mutex;
    pthread_cond_t  _cond_productor;
    pthread_cond_t  _cond_consumer;
};

void* thr_consumer (void *arg)
{
    BlockQueue *q = (BlockQueue*)arg;
    while(1) {
    int  data;
    q->QueuePop(&data);
    std::cout<<"consumer  get data:"<<data<<std::endl;
    }
    return NULL;
}
void* thr_productor(void *arg)
{
    int i = 0;
    BlockQueue *q = (BlockQueue*)arg;
    while(1) {
    std::cout<<"productor put data:"<<i<<std::endl;
    q->QueuePush(i++);
    }
    return NULL;
}
int main (int argc, char *argv[])
{
    pthread_t ctid[4], ptid[4];
    BlockQueue q;
    int ret;

    for(int i = 0; i < 4; i++) {
    ret = pthread_create(&ctid[i], NULL, thr_consumer, (void*)&q);
    if (ret != 0) {
        std::cout<<"pthread create error\n";
    }
    }
    for(int i = 0; i < 4; i++) {
    ret = pthread_create(&ptid[i], NULL, thr_productor, (void*)&q);
    if (ret != 0) {
        std::cout<<"pthread create error\n";
    }
    }
    for(int i = 0; i < 4; i++) {
    pthread_join(ctid[i], NULL);
    }
    for(int i = 0; i < 4; i++) {
    pthread_join(ptid[i], NULL);
    }
    return 0;
}

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值