多生产多消费的阻塞队列

Makefile

cpp 复制代码
ConProd:ConProd.cc
	gcc -o ConProd ConProd.cc -std=c++11 -lstdc++ -pthread
 
.PHONY:clean
clean:
	rm -f ConProd

lockGuard.hpp

cpp 复制代码
#pragma once

#include <iostream>
#include <pthread.h>
class Mutex
{
public:
    Mutex(pthread_mutex_t *pmtx)
        : _pmtx(pmtx)
    {
    }

    void *lock()
    {
        pthread_mutex_lock(_pmtx);
    }

    void unlock()
    {
        pthread_mutex_unlock(_pmtx);
    }

    ~Mutex()
    {
    }

    // private:
    pthread_mutex_t *_pmtx;
};

class lockGuard
{
public:
    lockGuard(pthread_mutex_t *pmtx)
        : _mtx(pmtx)
    {
        _mtx.lock();
    }
    ~lockGuard()
    {
        _mtx.unlock();
    }

private:
    Mutex _mtx;
};

Task.hpp

cpp 复制代码
#pragma once

#include <iostream>
#include <functional>

class Task
{
    typedef std::function<int(int, int)> func_t;

public:
    Task()
    {
    }
    Task(int x, int y, func_t func)
        : _x(x), _y(y), _func(func)
    {
    }

    int operator()()
    {
        return _func(_x, _y);
    }

    // private:
    int _x;
    int _y;
    func_t _func;
};

BlockQueue.hpp

cpp 复制代码
#pragma once

#include <iostream>
#include <queue>
#include <pthread.h>
#include <unistd.h>
#include "lockGuard.hpp"

const int gDefaultCap = 5;

template <class T>
class BlockQueue
{
public:
    BlockQueue(int capacity = gDefaultCap)
        : _capacity(capacity)
    {
        pthread_mutex_init(&_mtx, nullptr);
        pthread_cond_init(&_Empty, nullptr);
        pthread_cond_init(&_Full, nullptr);
    }

    bool Empty()
    {
        return _bq.empty();
    }

    bool Full()
    {
        return _bq.size() == _capacity;
    }

    void push(const T &in)
    {
        lockGuard lock(&_mtx);
        while (Full())
        {
            pthread_cond_wait(&_Full, &_mtx);
        }
        _bq.push(in);
        sleep(1);
        pthread_cond_signal(&_Empty);
    }

    void pop(T *out)
    {
        lockGuard lock(&_mtx);
        while (Empty())
        {
            pthread_cond_wait(&_Empty, &_mtx);
        }
        *out = _bq.front();
        _bq.pop();
        pthread_cond_signal(&_Full);
    }

    ~BlockQueue()
    {
        pthread_mutex_destroy(&_mtx);
        pthread_cond_destroy(&_Empty);
        pthread_cond_destroy(&_Full);
    }

private:
    std::queue<T> _bq;     // 阻塞队列
    int _capacity;         // 容量上限
    pthread_mutex_t _mtx;  // 通过互斥锁保证队列安全
    pthread_cond_t _Empty; // 用它来表示bq是否为空的条件
    pthread_cond_t _Full;  // 用它来表示bq是否为满的条件
};

ConProd.cc

cpp 复制代码
#include "BlockQueue.hpp"
#include "Task.hpp"

#include <pthread.h>
#include <unistd.h>
#include <time.h>

int Add(int x, int y)
{
    return x + y;
}

void *consumer(void *args)
{
    BlockQueue<Task> *bqueue = (BlockQueue<Task> *)args;
    Task task;
    while (true)
    {
        bqueue->pop(&task);
        std::cout << pthread_self() << " : " << task._x << " + " << task._y << "=" << task() << std::endl;
    }
    return nullptr;
}

void *productor(void *args)
{
    BlockQueue<Task> *bqueue = (BlockQueue<Task> *)args;
    while (true)
    {
        // int x, y;
        // std::cout << "Please Enter x:";
        // std::cin >> x;
        // std::cout << "Please Enter y:";
        // std::cin >> y;
        int x = rand() % 1000 + 1;
        usleep(rand() % 1000);
        int y = rand() % 1500 + 1;
        Task task(x, y, Add);
        bqueue->push(task);
        std::cout << pthread_self() << " : " << x << " + " << y << " =?" << std::endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    srand((unsigned int)time(nullptr));
    BlockQueue<Task> *bqueue = new BlockQueue<Task>(10);

    pthread_t c[3], p[3];
    for (int i = 0; i < 3; i++)
    {
        pthread_create(c + i, nullptr, consumer, bqueue);
    }

    for (int i = 0; i < 3; i++)
    {
        pthread_create(p + i, nullptr, productor, bqueue);
    }

    for (int i = 0; i < 3; i++)
    {
        pthread_join(c[i], nullptr);
    }

    for (int i = 0; i < 3; i++)
    {
        pthread_join(p[i], nullptr);
    }
    delete bqueue;

    return 0;
}
相关推荐
sp_fyf_202413 分钟前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-02
人工智能·神经网络·算法·计算机视觉·语言模型·自然语言处理·数据挖掘
我是哈哈hh2 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
Tisfy2 小时前
LeetCode 2187.完成旅途的最少时间:二分查找
算法·leetcode·二分查找·题解·二分
Mephisto.java2 小时前
【力扣 | SQL题 | 每日四题】力扣2082, 2084, 2072, 2112, 180
sql·算法·leetcode
robin_suli2 小时前
滑动窗口->dd爱框框
算法
丶Darling.2 小时前
LeetCode Hot100 | Day1 | 二叉树:二叉树的直径
数据结构·c++·学习·算法·leetcode·二叉树
labuladuo5202 小时前
Codeforces Round 977 (Div. 2) C2 Adjust The Presentation (Hard Version)(思维,set)
数据结构·c++·算法
jiyisuifeng19913 小时前
代码随想录训练营第54天|单调栈+双指针
数据结构·算法
꧁༺❀氯ྀൢ躅ྀൢ❀༻꧂3 小时前
实验4 循环结构
c语言·算法·基础题
新晓·故知3 小时前
<基于递归实现线索二叉树的构造及遍历算法探讨>
数据结构·经验分享·笔记·算法·链表