日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

muduo之BlockingQueue

發(fā)布時間:2025/6/15 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 muduo之BlockingQueue 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

? ? ? ? ?BlockingQueue是muduo是無界隊列,利用隊列(deque)實現(xiàn),向隊列中加入和取出元素用互斥量和條件變量結(jié)合的方式來操作,就是一個線程同步的問題。

BlockingQueue.h

// Use of this source code is governed by a BSD-style license // that can be found in the License file. // // Author: Shuo Chen (chenshuo at chenshuo dot com)#ifndef MUDUO_BASE_BLOCKINGQUEUE_H #define MUDUO_BASE_BLOCKINGQUEUE_H#include "muduo/base/Condition.h" #include "muduo/base/Mutex.h"#include <deque> #include <assert.h>namespace muduo { //互斥鎖配合條件變量是為了防止線程不停的主動獲得鎖、檢查條件、釋放鎖、再獲得鎖、再檢查、再釋放,一直到滿足運行的條件的時候才行 template<typename T> class BlockingQueue : noncopyable //無界阻塞隊列 {public:BlockingQueue(): mutex_(),notEmpty_(mutex_), //條件變量queue_(){}void put(const T& x){MutexLockGuard lock(mutex_);queue_.push_back(x);notEmpty_.notify(); // wait morphing saves us// http://www.domaigne.com/blog/computing/condvars-signal-with-mutex-locked-or-not/}void put(T&& x){MutexLockGuard lock(mutex_);queue_.push_back(std::move(x));notEmpty_.notify();//向所有線程提示條件已發(fā)生}T take(){MutexLockGuard lock(mutex_);// always use a while-loop, due to spurious wakeupwhile (queue_.empty()){notEmpty_.wait(); //等待條件發(fā)生}assert(!queue_.empty());T front(std::move(queue_.front()));//掏空queue_.front(),queue_.front()就變?yōu)榭樟藂ueue_.pop_front();return std::move(front);}size_t size() const{MutexLockGuard lock(mutex_);return queue_.size();}private:mutable MutexLock mutex_;Condition notEmpty_ GUARDED_BY(mutex_);std::deque<T> queue_ GUARDED_BY(mutex_); };} // namespace muduo#endif // MUDUO_BASE_BLOCKINGQUEUE_H

?

總結(jié)

以上是生活随笔為你收集整理的muduo之BlockingQueue的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。