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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

muduo之EventLoopThreadPool

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

? ? ? ? ??EventLoopThreadPool是IO線程對應的線程池,通過調用start函數來new EventLoopThread創建對應的線程和其loop,并將創建的保存在vector中。創建TcpConnection時,就會從EventLoopThreadPool中獲取一個IO線程。

EventLoopThreadPool.h

// Copyright 2010, Shuo Chen. All rights reserved. // http://code.google.com/p/muduo/ // // 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) // // This is an internal header file, you should not include this.#ifndef MUDUO_NET_EVENTLOOPTHREADPOOL_H #define MUDUO_NET_EVENTLOOPTHREADPOOL_H#include "muduo/base/noncopyable.h" #include "muduo/base/Types.h"#include <functional> #include <memory> #include <vector>namespace muduo {namespace net {class EventLoop; class EventLoopThread;class EventLoopThreadPool : noncopyable {public:typedef std::function<void(EventLoop*)> ThreadInitCallback;EventLoopThreadPool(EventLoop* baseLoop, const string& nameArg);~EventLoopThreadPool();void setThreadNum(int numThreads) { numThreads_ = numThreads; }void start(const ThreadInitCallback& cb = ThreadInitCallback());// valid after calling start()/// round-robinEventLoop* getNextLoop();/// with the same hash code, it will always return the same EventLoopEventLoop* getLoopForHash(size_t hashCode);std::vector<EventLoop*> getAllLoops();bool started() const{ return started_; }const string& name() const{ return name_; }private:EventLoop* baseLoop_; //主線程的事件驅動循環,TcpServer所在的事件驅動循環,創建TcpServer傳入的EventLoopstring name_;bool started_;int numThreads_; //線程數 int next_; //標記下次應該取出哪個線程,采用round_robinstd::vector<std::unique_ptr<EventLoopThread>> threads_; //線程池中所有的線程 //線程池中每個線程對應的事件驅動循環,從線程池取出線程實際上返回的是事件驅動循環//每個事件驅動循環運行在一個線程中std::vector<EventLoop*> loops_; };} // namespace net } // namespace muduo#endif // MUDUO_NET_EVENTLOOPTHREADPOOL_H

EventLoopThreadPool.cc

// Copyright 2010, Shuo Chen. All rights reserved. // http://code.google.com/p/muduo/ // // 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)#include "muduo/net/EventLoopThreadPool.h"#include "muduo/net/EventLoop.h" #include "muduo/net/EventLoopThread.h"#include <stdio.h>using namespace muduo; using namespace muduo::net;//EventLoopThreadPool loop對應的線程池 EventLoopThreadPool::EventLoopThreadPool(EventLoop* baseLoop, const string& nameArg): baseLoop_(baseLoop),name_(nameArg),started_(false),numThreads_(0),next_(0) { }EventLoopThreadPool::~EventLoopThreadPool() {// Don't delete loop, it's stack variable }void EventLoopThreadPool::start(const ThreadInitCallback& cb) //開啟線程池,創建線程 {assert(!started_);baseLoop_->assertInLoopThread();started_ = true;for (int i = 0; i < numThreads_; ++i){char buf[name_.size() + 32];snprintf(buf, sizeof buf, "%s%d", name_.c_str(), i);EventLoopThread* t = new EventLoopThread(cb, buf);threads_.push_back(std::unique_ptr<EventLoopThread>(t));loops_.push_back(t->startLoop());}if (numThreads_ == 0 && cb){cb(baseLoop_);} }EventLoop* EventLoopThreadPool::getNextLoop() //獲取一個線程(事件驅動循環),通常在創建TcpConnection時調用 {baseLoop_->assertInLoopThread();assert(started_);EventLoop* loop = baseLoop_;if (!loops_.empty()){// round-robinloop = loops_[next_];++next_;if (implicit_cast<size_t>(next_) >= loops_.size()){next_ = 0;}}return loop; }EventLoop* EventLoopThreadPool::getLoopForHash(size_t hashCode) {baseLoop_->assertInLoopThread();EventLoop* loop = baseLoop_;if (!loops_.empty()){loop = loops_[hashCode % loops_.size()];}return loop; }std::vector<EventLoop*> EventLoopThreadPool::getAllLoops() {baseLoop_->assertInLoopThread();assert(started_);if (loops_.empty()){return std::vector<EventLoop*>(1, baseLoop_);}else{return loops_;} }

?

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。