基于循环队列和信号量的生产和消费者模型

这一节为什么要基于信号量来实现同一个模型,原因:

 void push(const T& in)
    {
        pthread_mutex_lock(&_lock);
        while(is_Full())
        {
            //这里说明阻塞队列是满的,需要让生产者等待
            pthread_cond_wait(&_pcond,&_lock);
            
        }
        //这里说明阻塞队列至少有一个空位可以插入
        _queue.push(in);
        //唤醒消费者去消费
        pthread_cond_signal(&_ccond);
        pthread_mutex_unlock(&_lock);
        
    }

在我们访问公共资源的时候,消费者需要竞争同一把锁,然后还要继续判断是否能在临界区中生产数据。如果竞争到了锁然后判断不能生产数据,则需要继续等待。竞争锁需要消耗时间,判断等待也需要,这就导致了程序效率的低下。因此信号量的存在就解决了这一个问题:在竞争锁之前就提前预知了临界资源是否就绪。

POSIX信号量

信号量就相当于一个计数器,计数了临界资源中资源的数目,接下来我们学习使用它的接口:

初始化信号量

#include <semaphore.h>
int sem_init(sem_t *sem, int pshared, unsigned int value);
参数:
pshared:0 表示线程间共享,非零表示进程间共享
value :信号量初始值

销毁信号量

int sem_destroy(sem_t *sem);

等待信号量

功能:等待信号量,会将信号量的值减 1
int sem_wait(sem_t *sem); //P()

发布信号量

功能:发布信号量,表示资源使用完毕,可以归还资源了。将信号量值加 1
int sem_post(sem_t *sem);//V()

基队于环形列的生产消型费模型

引入循环队列

循环队列是一个逻辑结构,是由数组演变而成。通过对数组的下标访问进行模运算变成一个循环访问,这就是循环队列。当消费者和生产者在同一位置时,可能循环队列为空,另一种可能就是生产者消费满了,消费者来不及消费。因此其余时间生产消费者都不在同一位置生产和消费数据。

实现模型的三个必要条件

1.生产者不能套消费者一圈。

2.消费者不能超越生产者。

3.如果生产者和消费者在同一个位置,队列为空让生产者先走,队列为满让消费者先走。

引入信号量

通过信号量我们可以预知知道队列中资源的数目,如果生产者套了消费者一圈,这时生产者就申请不到信号量,只有消费者能够申请,因此只有消费者可以消费,生产者必须等待。同理消费者把数据消费光和生产者在同一位置时,消费者申请不到信号量只有生产者可以,因此只有生产者可以生产而消费者必须等待,通过引入信号量满足了该模型的三个必要条件。

代码实现

简易版的代码实现:

main.cc

#include <iostream>
#include <pthread.h>
#include <time.h>
#include <cstdlib>
#include <unistd.h>
#include "RingQueue.hpp"

using namespace std;

void *consumer(void *queue)
{
    RingQueue<int> *rq = static_cast<RingQueue<int> *>(queue);
    while (true)
    {
        int result = 0;
        rq->pop(&result);
        cout << "消费者消费了一个数据:" << result << endl;
        sleep(1);
    }
}

void *product(void *queue)
{
    RingQueue<int> *rq = static_cast<RingQueue<int> *>(queue);
    while (true)
    {
        //(1);
        int data = rand() % 10 + 1;
        rq->push(data);
        cout << "生产者生产了一个数据:" << data << endl;
    }
}

int main()
{
    srand((unsigned int)time(nullptr) ^ getpid() ^ 0x123456);
    RingQueue<int> *rq = new RingQueue<int>();
    pthread_t c, p;
    pthread_create(&c, nullptr, consumer, (void *)rq);
    pthread_create(&p, nullptr, product, (void *)rq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);
    delete rq;
    return 0;
}

RingQueue.hpp 

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

using namespace std;

static const int gcap = 6;

template <class T>
class RingQueue
{

private:
    void P(sem_t *sem) // 申请信号量 --
    {
        int n = sem_wait(sem);
        assert(n == 0);
        (void)n;
    }

    void V(sem_t *sem) // 释放信号量 ++
    {
        int n = sem_post(sem);
        assert(n == 0);
        (void)n;
    }

public:
    RingQueue(const int &cap = gcap) : _queue(cap), _cap(cap)
    {

        int n = sem_init(&_spaceSem, 0, cap);
        assert(n == 0);
        int m = sem_init(&_dataSem, 0, 0);
        assert(m == 0);
        _productStep = _consumerStep = 0;
    }

    void push(const T &in)
    {
        P(&_spaceSem); // 申请空间信号量
        _queue[_productStep++] = in;
        _productStep %= _cap;

        V(&_dataSem); // 释放数据信号量
    }

    void pop(T *out)
    {
        P(&_dataSem);
        *out = _queue[_consumerStep++];
        _consumerStep %= _cap;

        V(&_spaceSem);
    }

    ~RingQueue()
    {
        sem_destroy(&_spaceSem);
        sem_destroy(&_dataSem);
    }

private:
    std::vector<T> _queue;
    int _cap;
    sem_t _spaceSem;
    sem_t _dataSem;
    int _productStep;
    int _consumerStep;
};

引入计算任务:

Task.hpp

#include <iostream>
#include <functional>
#include <string>

class CalTask
{
public:
    using func_t = std::function<int(int, int, char)>;

    CalTask() {}

    CalTask(int x, int y, char op, func_t func) : _x(x), _y(y), _op(op), _callback(func)
    {
    }

    std::string operator()()
    {
        int result = _callback(_x, _y, _op);
        char buffer[64];
        snprintf(buffer, sizeof buffer, "%d %c %d =%d", _x, _op, _y, result);
        return buffer;
    }

    std::string to_string()
    {
        char buffer[64];
        snprintf(buffer, sizeof buffer, "%d %c %d = ?", _x, _op, _y);
        return buffer;
    }

    ~CalTask()
    {
    }

private:
    int _x;
    int _y;
    char _op;
    func_t _callback;
};

int mymath(int x, int y, char op)
{
    int result = 0;
    switch (op)
    {
    case '+':
        result = x + y;
        break;
    case '-':
        result = x - y;
        break;
    case '*':
        result = x * y;
        break;

    case '/':
        if (y == 0)
        {
            std::cerr << "div zero error" << std::endl;
            break;
        }
        result = x / y;
        break;

    case '%':
        if (y == 0)
        {
            std::cerr << "mod zero error" << std::endl;
            break;
        }
        result = x % y;
        break;
    }
    return result;
}

RingQueue.hpp

#include <iostream>
#include <semaphore.h>
#include <vector>
#include <string>
#include <cassert>

using namespace std;

static const int gcap = 6;
static const string symbol ="+-*/%";

template <class T>
class RingQueue
{

private:
    void P(sem_t *sem) // 申请信号量 --
    {
        int n = sem_wait(sem);
        assert(n == 0);
        (void)n;
    }

    void V(sem_t *sem) // 释放信号量 ++
    {
        int n = sem_post(sem);
        assert(n == 0);
        (void)n;
    }

public:
    RingQueue(const int &cap = gcap) : _queue(cap), _cap(cap)
    {

        int n = sem_init(&_spaceSem, 0, cap);
        assert(n == 0);
        int m = sem_init(&_dataSem, 0, 0);
        assert(m == 0);
        _productStep = _consumerStep = 0;
    }

    void push(const T &in)
    {
        P(&_spaceSem); // 申请空间信号量
        _queue[_productStep++] = in;
        _productStep %= _cap;

        V(&_dataSem); // 释放数据信号量
    }

    void pop(T *out)
    {
        P(&_dataSem);
        *out = _queue[_consumerStep++];
        _consumerStep %= _cap;

        V(&_spaceSem);
    }

    ~RingQueue()
    {
        sem_destroy(&_spaceSem);
        sem_destroy(&_dataSem);
    }

private:
    std::vector<T> _queue;
    int _cap;
    sem_t _spaceSem;
    sem_t _dataSem;
    int _productStep;
    int _consumerStep;
};

main.cc

#include <iostream>
#include <pthread.h>
#include <time.h>
#include <cstdlib>
#include <unistd.h>
#include "RingQueue.hpp"
#include "Task.hpp"

using namespace std;

void *consumer(void *queue)
{
    RingQueue<CalTask> *rq = static_cast<RingQueue<CalTask> *>(queue);
    while (true)
    {
        CalTask result ;
        rq->pop(&result);
        cout << "消费者处理了一个任务:" << result() << endl;
        sleep(1);
    }
}

void *product(void *queue)
{
    RingQueue<CalTask> *rq = static_cast<RingQueue<CalTask> *>(queue);
    while (true)
    {
        int x =rand()%10;
        int y =rand()%10;
        char op =symbol[rand()%symbol.size()];
        CalTask t(x,y,op, mymath);
        rq->push(t);
        cout << "生产者生产了一个任务:" << t.to_string() << endl;
    }
}

int main()
{
    srand((unsigned int)time(nullptr) ^ getpid() ^ 0x123456);
    RingQueue<CalTask> *rq = new RingQueue<CalTask>();
    pthread_t c, p;
    pthread_create(&c, nullptr, consumer, (void *)rq);
    pthread_create(&p, nullptr, product, (void *)rq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);
    delete rq;
    return 0;
}

多线程多并发执行:

想要实现多线程并发的进行消费和生产,我们只需要保证临界资源的访问安全,因此我们可以在访问临界资源的时候加上锁:

但是这么加锁有一个问题:当我们申请锁成功后才能申请信号量,锁只有一把需要多个线程竞争后才能交付并且申请信号量也需要花费时间。因此我们可以先申请信号量,让多个线程先预定好共享资源中的资源,然后再申请锁进行临界资源的访问,这样做提高了程序的效率。

最终代码:

//RingQueue.hpp

#include <iostream>
#include <semaphore.h>
#include <vector>
#include <string>
#include <cassert>

using namespace std;

static const int gcap = 6;
static const string symbol ="+-*/%";

template <class T>
class RingQueue
{

private:
    void P(sem_t *sem) // 申请信号量 --
    {
        int n = sem_wait(sem);
        assert(n == 0);
        (void)n;
    }

    void V(sem_t *sem) // 释放信号量 ++
    {
        int n = sem_post(sem);
        assert(n == 0);
        (void)n;
    }

public:
    RingQueue(const int &cap = gcap) : _queue(cap), _cap(cap)
    {

        int n = sem_init(&_spaceSem, 0, cap);
        assert(n == 0);
        int m = sem_init(&_dataSem, 0, 0);
        assert(m == 0);
        _productStep = _consumerStep = 0;
        pthread_mutex_init(&_pmutex,nullptr);
        pthread_mutex_init(&_cmutex,nullptr);

    }

    void push(const T &in)
    {
        P(&_spaceSem); // 申请空间信号量
        pthread_mutex_lock(&_pmutex);
        _queue[_productStep++] = in;
        _productStep %= _cap;
        pthread_mutex_unlock(&_pmutex);

        V(&_dataSem); // 释放数据信号量
        

    }

    void pop(T *out)
    {
        
        P(&_dataSem);
        pthread_mutex_lock(&_cmutex);
        *out = _queue[_consumerStep++];
        _consumerStep %= _cap;
        pthread_mutex_unlock(&_cmutex);
        
        V(&_spaceSem);
    
    }

    ~RingQueue()
    {
        sem_destroy(&_spaceSem);
        sem_destroy(&_dataSem);
        pthread_mutex_destroy(&_pmutex);
        pthread_mutex_destroy(&_cmutex);

    }

private:
    std::vector<T> _queue;
    int _cap;
    sem_t _spaceSem;
    sem_t _dataSem;
    int _productStep;
    int _consumerStep;
    pthread_mutex_t _pmutex;
    pthread_mutex_t _cmutex;
};

//Task.hpp

#include <iostream>
#include <functional>
#include <string>

class CalTask
{
public:
    using func_t = std::function<int(int, int, char)>;

    CalTask() {}

    CalTask(int x, int y, char op, func_t func) : _x(x), _y(y), _op(op), _callback(func)
    {
    }

    std::string operator()()
    {
        int result = _callback(_x, _y, _op);
        char buffer[64];
        snprintf(buffer, sizeof buffer, "%d %c %d =%d", _x, _op, _y, result);
        return buffer;
    }

    std::string to_string()
    {
        char buffer[64];
        snprintf(buffer, sizeof buffer, "%d %c %d = ?", _x, _op, _y);
        return buffer;
    }

    ~CalTask()
    {
    }

private:
    int _x;
    int _y;
    char _op;
    func_t _callback;
};

int mymath(int x, int y, char op)
{
    int result = 0;
    switch (op)
    {
    case '+':
        result = x + y;
        break;
    case '-':
        result = x - y;
        break;
    case '*':
        result = x * y;
        break;

    case '/':
        if (y == 0)
        {
            std::cerr << "div zero error" << std::endl;
            break;
        }
        result = x / y;
        break;

    case '%':
        if (y == 0)
        {
            std::cerr << "mod zero error" << std::endl;
            break;
        }
        result = x % y;
        break;
    }
    return result;
}

//main.cc

#include <iostream>
#include <pthread.h>
#include <time.h>
#include <cstdlib>
#include <unistd.h>
#include "RingQueue.hpp"
#include "Task.hpp"

using namespace std;

string SelfName()
{
    char name[64];
    snprintf(name,sizeof name,"thread[0x%x]",pthread_self());
    return name;
}

void *consumer(void *queue)
{
    RingQueue<CalTask> *rq = static_cast<RingQueue<CalTask> *>(queue);
    while (true)
    {
        CalTask result ;
        rq->pop(&result);
        cout << SelfName()<<"消费者处理了一个任务:" << result() << endl;
        sleep(1);
    }
}

void *product(void *queue)
{
    RingQueue<CalTask> *rq = static_cast<RingQueue<CalTask> *>(queue);
    while (true)
    {
        int x =rand()%10;
        int y =rand()%10;
        char op =symbol[rand()%symbol.size()];
        CalTask t(x,y,op, mymath);
        rq->push(t);
        cout << SelfName()<<"生产者生产了一个任务:" << t.to_string() << endl;
    }
}

int main()
{
    srand((unsigned int)time(nullptr) ^ getpid() ^ 0x123456);
    RingQueue<CalTask> *rq = new RingQueue<CalTask>();
    pthread_t c[10], p[6];
    for(int i =0;i<10;++i)pthread_create(c+i, nullptr, consumer, (void *)rq);
    for(int i =0;i<6;++i)pthread_create(p+i, nullptr, product, (void *)rq);

    for(int i =0;i<10;++i)pthread_join(c[i], nullptr);
    for(int i =0;i<6;++i)pthread_join(p[i], nullptr);
    
    delete rq;
    return 0;
}

这里所说的消费者和生产者模型的高效性和上一节基于阻塞队列的是一样的,如果不清楚的话可以点击《基于阻塞队列的生产和消费模型》进行复习!!!到这里生产者和消费者模型讲解就结束了,下一节我们学习线程池,敬请期待!!!!!记得点赞多多支持喔!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值