C++併發實戰12:線程安全的queue

 http://blog.csdn.net/liuxuejiang158blog/article/details/17301739
分類: C++併發實戰 793人閱讀 評論(5) 收藏 舉報

1 首先看下STL中的queue的接口:

  1. template <class T, class Container = std::deque<T> >  
  2. class queue {  
  3.   public:  
  4.     explicit queue(const Container&);  
  5.     explicit queue(Container&& = Container());  
  6.     template <class Alloc> explicit queue(const Alloc&);  
  7.     template <class Alloc> queue(const Container&, const Alloc&);  
  8.     template <class Alloc> queue(Container&&, const Alloc&);  
  9.     template <class Alloc> queue(queue&&, const Alloc&);  
  10.     void swap(queue& q);  
  11.     bool empty() const;  
  12.     size_type size() const;  
  13.     T& front();  
  14.     const T& front() const;  
  15.     T& back();  
  16.     const T& back() const;  
  17.     void push(const T& x);  
  18.     void push(T&& x);  
  19.     void pop();  
  20.     template <class... Args> void emplace(Args&&... args);  
  21. };  

      STL中的queue是非線程安全的,一個組合操作:front(); pop()先讀取隊首元素然後刪除隊首元素,若是有多個線程執行這個組合操作的話,可能會發生執行序列交替執行,導致一些意想不到的行爲。因此需要重新設計線程安全的queue的接口。

2  簡化的線程安全的queue接口

  1. #include <memory>   
  2. template<typename T>  
  3. class threadsafe_queue  
  4. {  
  5.   public:  
  6.     threadsafe_queue();  
  7.     threadsafe_queue(const threadsafe_queue&);  
  8.     threadsafe_queue& operator=(const threadsafe_queue&) = delete;//禁止賦值操作是爲了簡化   
  9.     void push(T new_value);  
  10.     bool try_pop(T& value);//嘗試刪除隊首元素,若刪除成功則通過value返回隊首元素,並返回true;若隊爲空,則返回false   
  11.     std::shared_ptr<T> try_pop();//若隊非空shared_ptr返回並刪除的隊首元素;若隊空,則返回的shared_ptr爲NULL  
  12.     void wait_and_pop(T& value);//若隊非空,通過value返回隊首元素並刪除,函數返回true;若隊爲空,則通過condition_variable等待有元素入隊後再獲取閉並刪除隊首元素  
  13.     std::shared_ptr<T> wait_and_pop();//和前面一樣,只不過通過shared_ptr返回隊首元素  
  14.     bool empty() const;  
  15. };  


3 實現threadsafe_queue

  1. #include <queue>  
  2. #include <memory>  
  3. #include <mutex>  
  4. #include <condition_variable>  
  5. template<typename T>  
  6. class threadsafe_queue  
  7. {  
  8.   private:  
  9.      mutable std::mutex mut;   
  10.      std::queue<T> data_queue;  
  11.      std::condition_variable data_cond;  
  12.   public:  
  13.      threadsafe_queue(){}  
  14.      threadsafe_queue(threadsafe_queue const& other)  
  15.      {  
  16.          std::lock_guard<std::mutex> lk(other.mut);  
  17.          data_queue=other.data_queue;  
  18.      }  
  19.      void push(T new_value)//入隊操作  
  20.      {  
  21.          std::lock_guard<std::mutex> lk(mut);  
  22.          data_queue.push(new_value);  
  23.          data_cond.notify_one();  
  24.      }  
  25.      void wait_and_pop(T& value)//直到有元素可以刪除爲止  
  26.      {  
  27.          std::unique_lock<std::mutex> lk(mut);  
  28.          data_cond.wait(lk,[this]{return !data_queue.empty();});  
  29.          value=data_queue.front();  
  30.          data_queue.pop();  
  31.      }  
  32.      std::shared_ptr<T> wait_and_pop()  
  33.      {  
  34.          std::unique_lock<std::mutex> lk(mut);  
  35.          data_cond.wait(lk,[this]{return !data_queue.empty();});  
  36.          std::shared_ptr<T> res(std::make_shared<T>(data_queue.front()));  
  37.          data_queue.pop();  
  38.          return res;  
  39.      }  
  40.      bool try_pop(T& value)//不管有沒有隊首元素直接返回  
  41.      {  
  42.          std::lock_guard<std::mutex> lk(mut);  
  43.          if(data_queue.empty())  
  44.              return false;  
  45.          value=data_queue.front();  
  46.          data_queue.pop();  
  47.          return true;  
  48.      }  
  49.      std::shared_ptr<T> try_pop()  
  50.      {  
  51.          std::lock_guard<std::mutex> lk(mut);  
  52.          if(data_queue.empty())  
  53.              return std::shared_ptr<T>();  
  54.          std::shared_ptr<T> res(std::make_shared<T>(data_queue.front()));  
  55.          data_queue.pop();  
  56.          return res;  
  57.      }  
  58.      bool empty() const  
  59.      {  
  60.          std::lock_guard<std::mutex> lk(mut);  
  61.          return data_queue.empty();  
  62.      }  
  63. };  
發佈了5 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章