CUtilityCode

(1) 基于boost的生产者/消费者队列

template<typenameData>
classconcurrent_queue   

{   

private:  
 
    std::queue<Data> the_queue;  
 
    mutableboost::mutex the_mutex;   

    boost::condition_variable the_condition_variable;  
 
public:  
 
    void push(Data const& data)  
 
    {  
 
        boost::mutex::scoped_lock lock(the_mutex);  
 
        the_queue.push(data);  
 
        lock.unlock();  
 
        the_condition_variable.notify_one();  
 
    }  
 
    bool empty() const  
 
    {  
 
        boost::mutex::scoped_lock lock(the_mutex);  
 
        returnthe_queue.empty();   

    }  
 
    bool try_pop(Data& popped_value)   

    {  
 
        boost::mutex::scoped_lock lock(the_mutex);  
 
        if(the_queue.empty())  
 
        {  
 
            returnfalse;  
 
        }  
 
            
 
        popped_value=the_queue.front();  
 
        the_queue.pop();  
 
        returntrue;  
 
    }  
 
    void wait_and_pop(Data& popped_value)
    {  
 
        boost::mutex::scoped_lock lock(the_mutex);  
 
        while(the_queue.empty())  
 
        {  
 
            the_condition_variable.wait(lock);  
 
        }  
 
            
 
        popped_value=the_queue.front();  
 
        the_queue.pop();  
 
    }  
 
};

  

原文地址:https://www.cnblogs.com/wb-DarkHorse/p/6197324.html