简体   繁体   English

使用C ++标准队列阻止队列

[英]blocking queue using c++ std queue

I'm trying to implement a blocking queue with limited size, for one provider and multiple consumers. 我正在尝试为一个提供者和多个使用者实现一个有限大小的阻塞队列。 it is working well when the consumer is sleep()ing for 1 second, but hangs when there is no sleep. 消费者睡眠1秒钟时,它工作良好,但没有睡眠时挂起。 what am I doing wrong? 我究竟做错了什么?

here is my code: 这是我的代码:

    #include <iostream>
    #include <stdlib.h>
    #include <unistd.h>
    #include <thread>
    #include <queue>
    #include <mutex>
    #include <condition_variable>

    using namespace std;
    template <class T> class BlockingQueue: public queue<T> {
        public:
            BlockingQueue() {
                queue<T>();
            }

            BlockingQueue(int size) {
                maxSize = size;
                queue<T>();
            }

            void push(T item) {
                unique_lock<std::mutex> wlck(writerMutex);
                while(Full())
                    isFull.wait(wlck);
                queue<T>::push(item);
                if(notEmpty()) 
                    isEmpty.notify_one();
            }

            bool notEmpty() {
                return !queue<T>::empty();
            }

            bool Full(){
                return queue<T>::size() >= maxSize;
            }

        T pop() {
            unique_lock<std::mutex> lck(readerMutex);

            popMutex.lock();    
            while(queue<T>::empty()) {
                isEmpty.wait(lck);
            }
            T value = queue<T>::front();
            queue<T>::pop();
            if(!Full())
                isFull.notify_all();
            popMutex.unlock();
            return value;
        }

        private:
            int maxSize;
            std::mutex readerMutex;
            std::mutex popMutex;
            std::mutex writerMutex;
            condition_variable isFull;
            condition_variable isEmpty;
    };
    void runProvider(BlockingQueue<int>* Q) {
        int number=0;
        while(1) {
            Q->push(number);
            cout<<"provide "<<number<<endl;
            number++;
        }
    }

    void runConsumer(int n,BlockingQueue<int>* Q) {
        int number;
        while(1) {
            number = Q->pop();
            cout<<"consume#"<<n<<": "<<number<<endl;
        }
    }

    int main(int argc, char** argv) {
        BlockingQueue<int> *Queue = new BlockingQueue<int>(10);
        cout<<"starting provider"<<endl;
        std:thread provider(runProvider, Queue);
        sleep(1);

        cout<<"starting consumer"<<endl;
        std::thread consumer1(runConsumer, 1,Queue);
        std::thread consumer2(runConsumer, 2,Queue);

        provider.join();
        delete(Queue);
        return 0;
    }

Here is my fixed code for blocking queue with multiple providers and multiple consumers and limited queue size: 这是我的固定代码,用于阻止具有多个提供者和多个使用者且队列大小受限的队列:

template <class T> class BlockingQueue: public queue<T> {
    public:
        BlockingQueue(int size) {
            maxSize = size;
        }

        void push(T item) {
            unique_lock<std::mutex> wlck(writerMutex);
            while(Full())
                isFull.wait(wlck);
            queue<T>::push(item);
            isEmpty.notify_all();
        }

        bool notEmpty() {
            return !queue<T>::empty();
        }

        bool Full(){
            return queue<T>::size() >= maxSize;
        }

    T pop() {
        unique_lock<std::mutex> lck(readerMutex);
        while(queue<T>::empty()) {
            isEmpty.wait(lck);
        }
        T value = queue<T>::front();
        queue<T>::pop();
        if(!Full())
            isFull.notify_all();
        return value;
    }

    private:
        int maxSize;
        std::mutex readerMutex;
        std::mutex writerMutex;
        condition_variable isFull;
        condition_variable isEmpty;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM