muduo之EventLoopThread
生活随笔
收集整理的這篇文章主要介紹了
muduo之EventLoopThread
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ? ?muduo用EventLoopThread提供了對應eventloop和thread的封裝,意為I/O線程類,EventLoopThread可以創建一個IO線程,通過startLoop返回一個IO線程的loop,threadFunc中開啟loop循環,其中的API涉及一些多線程中的互斥量和條件變量的操作。
EventLoopThread.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 a public header file, it must only include public header files.#ifndef MUDUO_NET_EVENTLOOPTHREAD_H #define MUDUO_NET_EVENTLOOPTHREAD_H#include "muduo/base/Condition.h" #include "muduo/base/Mutex.h" #include "muduo/base/Thread.h"namespace muduo { namespace net {class EventLoop;//I/O線程類,線程池啟動IO線程 class EventLoopThread : noncopyable {public:typedef std::function<void(EventLoop*)> ThreadInitCallback;EventLoopThread(const ThreadInitCallback& cb = ThreadInitCallback(),const string& name = string());~EventLoopThread();EventLoop* startLoop(); //啟動成員thread_線程,該線程就成了I/O線程,內部調用thread_.start()private:void threadFunc(); //線程運行函數EventLoop* loop_ GUARDED_BY(mutex_); //loop_指針指向一個EventLoop對象bool exiting_;Thread thread_;MutexLock mutex_;Condition cond_ GUARDED_BY(mutex_);ThreadInitCallback callback_; //回調函數在EventLoop::loop事件循環之前被調用 };} // namespace net } // namespace muduo#endif // MUDUO_NET_EVENTLOOPTHREAD_HEventLoopThread.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/EventLoopThread.h"#include "muduo/net/EventLoop.h"using namespace muduo; using namespace muduo::net;EventLoopThread::EventLoopThread(const ThreadInitCallback& cb,const string& name): loop_(NULL), //loop未啟動為NULLexiting_(false),thread_(std::bind(&EventLoopThread::threadFunc, this), name), //綁定線程運行函數mutex_(),cond_(mutex_),callback_(cb) { }EventLoopThread::~EventLoopThread() {exiting_ = true;if (loop_ != NULL) // not 100% race-free, eg. threadFunc could be running callback_.{// still a tiny chance to call destructed object, if threadFunc exits just now.// but when EventLoopThread destructs, usually programming is exiting anyway.loop_->quit();thread_.join(); //等待線程退出} }EventLoop* EventLoopThread::startLoop() {assert(!thread_.started());thread_.start(); //調用pthread_create創建線程,此時有兩個線程在運行//一個是調用EventLoopThread::startLoop()的線程,一個是執行EventLoopThread::threadFunc()的線程(IO線程)EventLoop* loop = NULL;{MutexLockGuard lock(mutex_);while (loop_ == NULL){cond_.wait(); //須要等待EventLoop對象的創建}loop = loop_; //IO線程創建loop_賦給主線程}return loop; //主線程返回IO線程創建的EventLoop對象 }void EventLoopThread::threadFunc() //創建線程時會調用這個函數 {EventLoop loop; //IO線程也要創建EventLoop對象,還要通知主線程已經創建完畢if (callback_){callback_(&loop); //將定義好的loop傳入回調}{MutexLockGuard lock(mutex_);// loop_指針指向了一個棧上的對象,threadFunc函數退出之后。這個指針就失效了// threadFunc函數退出,就意味著線程退出了,EventLoopThread對象也就沒有存在的價值了// 因而不會有什么大的問題loop_ = &loop;cond_.notify(); //創建好,發送通知}loop.loop(); // 會在這里循環,直到EventLoopThread析構。此后不再使用loop_訪問EventLoop了//assert(exiting_);MutexLockGuard lock(mutex_);loop_ = NULL; }?
總結
以上是生活随笔為你收集整理的muduo之EventLoopThread的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: muduo之Socket和Sockets
- 下一篇: muduo之EventLoopThrea