有限容量BlockingQueue:消费者生产者

    有限容量的BlockingQueue实现工作队列,用于生产者消费者问题。

#include<iostream>
#include<string>
#include<vector>
#include<deque>
#include<assert.h>
#include<pthread.h>
#include<unistd.h>
#include<boost/noncopyable.hpp>
#include<boost/shared_ptr.hpp>
#include<boost/weak_ptr.hpp>
#include<boost/function.hpp>
#include<boost/bind.hpp>
#include<boost/circular_buffer.hpp>
using namespace std;
using namespace boost;
class Mutex:public noncopyable{//互斥量的封装
    public:
        Mutex(){
            pthread_mutex_init(&mutex,NULL);
        }
        void lock(){
            pthread_mutex_lock(&mutex);
        }
        void unlock(){
            pthread_mutex_unlock(&mutex);
        }
        ~Mutex(){
            pthread_mutex_destroy(&mutex);
        }
        pthread_mutex_t* getMutex(){
            return &mutex;
        }
    private:
        mutable pthread_mutex_t mutex;
};
class MutexLockGuard:noncopyable{//RAII管理互斥量
    public:
        explicit MutexLockGuard(Mutex& mutex):mutex_(mutex){
            mutex_.lock();
        }
        ~MutexLockGuard(){
            mutex_.unlock();
        }
    private:
        Mutex& mutex_;//注意是引用,Mutex继承了noncopyable后不能拷贝构造
};
class Condition:public noncopyable{//条件变量的封装
    public:
        explicit Condition(Mutex& mutex):mutex_(mutex){
            pthread_cond_init(&cond,NULL);
        }
        ~Condition(){
            pthread_cond_destroy(&cond);
        }
        void wait(){
            pthread_cond_wait(&cond,mutex_.getMutex());
        }
        void notify(){
            pthread_cond_signal(&cond);
        }
        void notifyALL(){
            pthread_cond_broadcast(&cond);
        }
    private:
        Mutex& mutex_;//注意是引用
        pthread_cond_t cond;
};
template<typename T>
class BlockingQueue:noncopyable{
    public:
        explicit BlockingQueue(int x):mutex(),full(mutex),empty(mutex),Q(x){}
        void put(T a){
            MutexLockGuard guard(mutex);
            while(Q.full()){//若队列满,则等待空条件empty
                empty.wait();//等待消费者消费
            }
            assert(!Q.full());
            Q.push_back(a);
            full.notifyALL();//通知消费者
        }
        T take(){
            MutexLockGuard guard(mutex);
            while(Q.empty()){//若队列空,则等待满条件full
                full.wait();//等待生产者生产
            }
            assert(!Q.empty());
            T front(Q.front());
            front();
            Q.pop_front();
            empty.notify();//通知生产者
            return front;
        }
    private:
        Mutex mutex;
        Condition full;//满条件
        Condition empty;//空条件
        circular_buffer<T> Q;//boost的循环队列
};
class test{//任务内容
    public:
        explicit test(int x):data(x){}
        void show(){
            cout<<"show "<<data<<endl;
        }
    private:
        int data;
};
typedef function<void()> Functor;//任务T
BlockingQueue<Functor> taskQueue(10);
bool running=true;//终止线程标志
void* producer(void* arg){//生产者线程
    int i=0;
    while(running){
        //usleep(100);
        test one(i++);
        Functor task=bind(&test::show,one);//注意bind会拷贝参数。该处不能用&one.
        taskQueue.put(task);
    }
}
void* customer(void* arg){//消费者线程
    while(running){
        Functor task=taskQueue.take();
        task();
    }
}
int main(){
    pthread_t pid0;
    pthread_t pid[2];
    pthread_create(&pid0,NULL,producer,NULL);
    for(int i=0;i<2;i++){
        pthread_create(&pid[i],NULL,customer,NULL);
    }
    sleep(1);
    running=false;//终止线程
    pthread_join(pid0,NULL);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值