1// excerpts from http://code.google.com/p/muduo/ 2// 3// Use of this source code is governed by a BSD-style license 4// that can be found in the License file. 5// 6// Author: Shuo Chen (chenshuo at chenshuo dot com) 7 8#include "EventLoopThread.h" 9 10#include "EventLoop.h" 11 12#include <boost/bind.hpp> 13 14using namespace muduo; 15 16EventLoopThread::EventLoopThread() 17 : loop_(NULL), 18 exiting_(false), 19 thread_(boost::bind(&EventLoopThread::threadFunc, this)), 20 mutex_(), 21 cond_(mutex_) 22{ 23} 24 25EventLoopThread::~EventLoopThread() 26{ 27 exiting_ = true; 28 loop_->quit(); 29 thread_.join(); 30} 31 32EventLoop* EventLoopThread::startLoop() 33{ 34 assert(!thread_.started()); 35 thread_.start(); 36 37 { 38 MutexLockGuard lock(mutex_); 39 while (loop_ == NULL) 40 { 41 cond_.wait(); 42 } 43 } 44 45 return loop_; 46} 47 48void EventLoopThread::threadFunc() 49{ 50 EventLoop loop; 51 52 { 53 MutexLockGuard lock(mutex_); 54 loop_ = &loop; 55 cond_.notify(); 56 } 57 58 loop.loop(); 59 //assert(exiting_); 60} 61 62