设为首页 加入收藏

TOP

muduo库如何支持多线程(一)
2013-11-20 14:23:42 来源: 作者: 【 】 浏览:945
Tags:muduo 如何 支持 线程

    muduo库如何支持多线程
    EventLoopThread(IO线程类)
    EventLoopThreadPool(IO线程池类)
    IO线程池的功能是开启若干个IO线程,并让这些IO线程处于事件循环的状态
    下面的这些代码可能和前面给出的源代码有些不一样,阅读的同学请注意了
    EventLoopThreadPool头文件
    eventloopthreadpool.h
    [cpp
    // 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/Condition.h>
    #include <muduo/base/Mutex.h>
    #include <vector>
    #include <boost/function.hpp>
    #include <boost/noncopyable.hpp>
    #include <boost/ptr_container/ptr_vector.hpp>
    namespace muduo
    {
    namespace net
    {
    class EventLoop;
    class EventLoopThread;
    class EventLoopThreadPool : boost::noncopyable
    {
    public:
    typedef boost::function<void(EventLoop*)> ThreadInitCallback;
    EventLoopThreadPool(EventLoop* baseLoop);
    ~EventLoopThreadPool();
    void setThreadNum(int numThreads) { numThreads_ = numThreads; }
    void start(const ThreadInitCallback& cb = ThreadInitCallback());
    EventLoop* getNextLoop();
    private:
    EventLoop* baseLoop_; // 与Acceptor所属EventLoop相同
    bool started_;        /*是否启动*/
    int numThreads_;      // 线程数
    int next_;            // 新连接到来,所选择的EventLoop对象下标
    boost::ptr_vector<EventLoopThread> threads_;        // IO线程列表
    std::vector<EventLoop*> loops_;                 // EventLoop列表
    };
    }
    }
    #endif  // MUDUO_NET_EVENTLOOPTHREADPOOL_H
    EventLoopThreadPool源文件
    eventloopthreadpool.cc
    [cpp]
    // 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 <boost/bind.hpp>
    using namespace muduo;
    using namespace muduo::net;
    EventLoopThreadPool::EventLoopThreadPool(EventLoop* baseLoop)
    : baseLoop_(baseLoop),  // 与Acceptor所属EventLoop相同
    started_(false),
    numThreads_(0),
    next_(0)
    {
    }
    EventLoopThreadPool::~EventLoopThreadPool()
    {
    // Don't delete loop, it's stack variable
    }

     

首页 上一页 1 2 3 4 5 6 7 下一页 尾页 1/13/13
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇二叉树的创建与递归遍历 下一篇快速幂和欧拉函数的优化求解

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·常用meta整理 | 菜鸟 (2025-12-25 01:21:52)
·SQL HAVING 子句:深 (2025-12-25 01:21:47)
·SQL CREATE INDEX 语 (2025-12-25 01:21:45)
·Shell 传递参数 (2025-12-25 00:50:45)
·Linux echo 命令 - (2025-12-25 00:50:43)