生产消费模型之环形队列

Makefile

cpp 复制代码
cp: testMain.cc
	g++ -o cp testMain.cc -std=c++11 -lpthread  
  
.PHONY: clean  
  
clean:  
	rm -rf cp

ringQueue.hpp

cpp 复制代码
#ifndef RINGQUEUE_HPP
#define RINGQUEUE_HPP

#include <iostream>
#include <vector>
#include <pthread.h>
#include "sem.hpp"
#include <unistd.h>

const int g_default_num = 5;

template <typename T>
class RingQueue
{
public:
    RingQueue(int default_num = g_default_num)
        : _ring_queue(default_num), _num(default_num), c_step(0), p_step(0), spaceSem(default_num), dataSem(0)
    {
        pthread_mutex_init(&clock, nullptr);
        pthread_mutex_init(&plock, nullptr);
    }
    void push(const T &in)
    {
        spaceSem.P();
        pthread_mutex_lock(&plock);
        _ring_queue[p_step++] = in;
        p_step %= _num;
        dataSem.V();
        pthread_mutex_unlock(&plock);
    }
    void pop(T *out)
    {
        dataSem.P();
        pthread_mutex_lock(&clock);
        *out = _ring_queue[c_step++];
        c_step %= _num;
        spaceSem.V();
        pthread_mutex_unlock(&clock);
    }
    void debug()
    {
        std::cerr << "size: " << _ring_queue.size() << " num: " << _num << std::endl;
    }
    ~RingQueue()
    {
        pthread_mutex_destroy(&clock);
        pthread_mutex_destroy(&plock);
    }

private:
    std::vector<T> _ring_queue;
    int _num;
    int c_step;   // 消费下标
    int p_step;   // 生产下标
    Sem spaceSem; // 空间资源
    Sem dataSem;  // 数据资源
    pthread_mutex_t clock;
    pthread_mutex_t plock;
};

#endif

sem.hpp

cpp 复制代码
#ifndef SEM_HPP
#define SEM_HPP

#include <iostream>
#include <semaphore.h>

class Sem
{
public:
    Sem(int value)
    {
        sem_init(&_sem, 0, value);
    }

    void P()
    {
        sem_wait(&_sem);
    }

    void V()
    {
        sem_post(&_sem);
    }

    ~Sem()
    {
        sem_destroy(&_sem);
    }

private:
    sem_t _sem;
};

#endif

testMain.cc

cpp 复制代码
#include "ringQueue.hpp"

void *consumer(void *arg)
{
    RingQueue<int> *rq = (RingQueue<int> *)arg;
    while (true)
    {
        sleep(1);
        int x;
        rq->pop(&x);
        std::cout << "消费:" << x << "[" << pthread_self() << "]" << std::endl;
    }
}

void *productor(void *arg)
{
    RingQueue<int> *rq = (RingQueue<int> *)arg;
    while (true)
    {
        int x = rand() % 100 + 1;
        rq->push(x);
        std::cout << "生产:" << x << "[" << pthread_self() << "]" << std::endl;

    }
}

int main()
{
    srand((unsigned int)time(nullptr));
    pthread_t c[3], p[2];
    RingQueue<int> *rq = new RingQueue<int>(4);
    rq->debug();
    pthread_create(c, nullptr, consumer, (void *)rq);
    pthread_create(c + 1, nullptr, consumer, (void *)rq);
    pthread_create(c + 2, nullptr, consumer, (void *)rq);

    pthread_create(p, nullptr, productor, (void *)rq);
    pthread_create(p + 1, nullptr, productor, (void *)rq);

    pthread_join(c[0], nullptr);
    pthread_join(c[1], nullptr);
    pthread_join(c[2], nullptr);

    pthread_join(p[0], nullptr);
    pthread_join(p[1], nullptr);
}
相关推荐
沐怡旸35 分钟前
【算法--链表】114.二叉树展开为链表--通俗讲解
算法·面试
一只懒洋洋1 小时前
K-meas 聚类、KNN算法、决策树、随机森林
算法·决策树·聚类
方案开发PCBA抄板芯片解密2 小时前
什么是算法:高效解决问题的逻辑框架
算法
songx_992 小时前
leetcode9(跳跃游戏)
数据结构·算法·游戏
小白狮ww3 小时前
RStudio 教程:以抑郁量表测评数据分析为例
人工智能·算法·机器学习
AAA修煤气灶刘哥3 小时前
接口又被冲崩了?Sentinel 这 4 种限流算法,帮你守住后端『流量安全阀』
后端·算法·spring cloud
kk”4 小时前
C语言快速排序
数据结构·算法·排序算法
纪元A梦4 小时前
贪心算法应用:基因编辑靶点选择问题详解
算法·贪心算法
3壹4 小时前
数据结构精讲:栈与队列实战指南
c语言·开发语言·数据结构·c++·算法
skytier5 小时前
Construct内报错和定位解决
算法