zoukankan      html  css  js  c++  java
  • 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();  
     
        }  
     
    };
    

      

  • 相关阅读:
    79. 滑动窗口的最大值
    78. 左旋转字符串
    77. 翻转单词顺序
    76. 和为S的连续正数序列
    75. 和为S的两个数字
    innodb 锁机制
    MVCC
    linux查看状态命令
    design pattern 资料整理
    mysql资料汇总
  • 原文地址:https://www.cnblogs.com/wb-DarkHorse/p/6197324.html
Copyright © 2011-2022 走看看