muduo之EventLoopThread
生活随笔
收集整理的這篇文章主要介紹了
muduo之EventLoopThread
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? ? ? ? ?muduo用EventLoopThread提供了對(duì)應(yīng)eventloop和thread的封裝,意為I/O線(xiàn)程類(lèi),EventLoopThread可以創(chuàng)建一個(gè)IO線(xiàn)程,通過(guò)startLoop返回一個(gè)IO線(xiàn)程的loop,threadFunc中開(kāi)啟loop循環(huán),其中的API涉及一些多線(xiàn)程中的互斥量和條件變量的操作。
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線(xiàn)程類(lèi),線(xiàn)程池啟動(dòng)IO線(xiàn)程 class EventLoopThread : noncopyable {public:typedef std::function<void(EventLoop*)> ThreadInitCallback;EventLoopThread(const ThreadInitCallback& cb = ThreadInitCallback(),const string& name = string());~EventLoopThread();EventLoop* startLoop(); //啟動(dòng)成員thread_線(xiàn)程,該線(xiàn)程就成了I/O線(xiàn)程,內(nèi)部調(diào)用thread_.start()private:void threadFunc(); //線(xiàn)程運(yùn)行函數(shù)EventLoop* loop_ GUARDED_BY(mutex_); //loop_指針指向一個(gè)EventLoop對(duì)象bool exiting_;Thread thread_;MutexLock mutex_;Condition cond_ GUARDED_BY(mutex_);ThreadInitCallback callback_; //回調(diào)函數(shù)在EventLoop::loop事件循環(huán)之前被調(diào)用 };} // 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未啟動(dòng)為NULLexiting_(false),thread_(std::bind(&EventLoopThread::threadFunc, this), name), //綁定線(xiàn)程運(yùn)行函數(shù)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(); //等待線(xiàn)程退出} }EventLoop* EventLoopThread::startLoop() {assert(!thread_.started());thread_.start(); //調(diào)用pthread_create創(chuàng)建線(xiàn)程,此時(shí)有兩個(gè)線(xiàn)程在運(yùn)行//一個(gè)是調(diào)用EventLoopThread::startLoop()的線(xiàn)程,一個(gè)是執(zhí)行EventLoopThread::threadFunc()的線(xiàn)程(IO線(xiàn)程)EventLoop* loop = NULL;{MutexLockGuard lock(mutex_);while (loop_ == NULL){cond_.wait(); //須要等待EventLoop對(duì)象的創(chuàng)建}loop = loop_; //IO線(xiàn)程創(chuàng)建loop_賦給主線(xiàn)程}return loop; //主線(xiàn)程返回IO線(xiàn)程創(chuàng)建的EventLoop對(duì)象 }void EventLoopThread::threadFunc() //創(chuàng)建線(xiàn)程時(shí)會(huì)調(diào)用這個(gè)函數(shù) {EventLoop loop; //IO線(xiàn)程也要?jiǎng)?chuàng)建EventLoop對(duì)象,還要通知主線(xiàn)程已經(jīng)創(chuàng)建完畢if (callback_){callback_(&loop); //將定義好的loop傳入回調(diào)}{MutexLockGuard lock(mutex_);// loop_指針指向了一個(gè)棧上的對(duì)象,threadFunc函數(shù)退出之后。這個(gè)指針就失效了// threadFunc函數(shù)退出,就意味著線(xiàn)程退出了,EventLoopThread對(duì)象也就沒(méi)有存在的價(jià)值了// 因而不會(huì)有什么大的問(wèn)題loop_ = &loop;cond_.notify(); //創(chuàng)建好,發(fā)送通知}loop.loop(); // 會(huì)在這里循環(huán),直到EventLoopThread析構(gòu)。此后不再使用loop_訪(fǎng)問(wèn)EventLoop了//assert(exiting_);MutexLockGuard lock(mutex_);loop_ = NULL; }?
總結(jié)
以上是生活随笔為你收集整理的muduo之EventLoopThread的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: muduo之Socket和Sockets
- 下一篇: muduo之EventLoopThrea