EventLoopThread.cc revision 2745a763
12745a763SShuo Chen// excerpts from http://code.google.com/p/muduo/ 22745a763SShuo Chen// 32745a763SShuo Chen// Use of this source code is governed by a BSD-style license 42745a763SShuo Chen// that can be found in the License file. 52745a763SShuo Chen// 62745a763SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 72745a763SShuo Chen 82745a763SShuo Chen#include "EventLoopThread.h" 92745a763SShuo Chen 102745a763SShuo Chen#include "EventLoop.h" 112745a763SShuo Chen 122745a763SShuo Chen#include <boost/bind.hpp> 132745a763SShuo Chen 142745a763SShuo Chenusing namespace muduo; 152745a763SShuo Chen 162745a763SShuo Chen 172745a763SShuo ChenEventLoopThread::EventLoopThread() 182745a763SShuo Chen : loop_(NULL), 192745a763SShuo Chen exiting_(false), 202745a763SShuo Chen thread_(boost::bind(&EventLoopThread::threadFunc, this)), 212745a763SShuo Chen mutex_(), 222745a763SShuo Chen cond_(mutex_) 232745a763SShuo Chen{ 242745a763SShuo Chen} 252745a763SShuo Chen 262745a763SShuo ChenEventLoopThread::~EventLoopThread() 272745a763SShuo Chen{ 282745a763SShuo Chen exiting_ = true; 292745a763SShuo Chen loop_->quit(); 302745a763SShuo Chen thread_.join(); 312745a763SShuo Chen} 322745a763SShuo Chen 332745a763SShuo ChenEventLoop* EventLoopThread::startLoop() 342745a763SShuo Chen{ 352745a763SShuo Chen assert(!thread_.started()); 362745a763SShuo Chen thread_.start(); 372745a763SShuo Chen 382745a763SShuo Chen { 392745a763SShuo Chen MutexLockGuard lock(mutex_); 402745a763SShuo Chen while (loop_ == NULL) 412745a763SShuo Chen { 422745a763SShuo Chen cond_.wait(); 432745a763SShuo Chen } 442745a763SShuo Chen } 452745a763SShuo Chen 462745a763SShuo Chen return loop_; 472745a763SShuo Chen} 482745a763SShuo Chen 492745a763SShuo Chenvoid EventLoopThread::threadFunc() 502745a763SShuo Chen{ 512745a763SShuo Chen EventLoop loop; 522745a763SShuo Chen 532745a763SShuo Chen { 542745a763SShuo Chen MutexLockGuard lock(mutex_); 552745a763SShuo Chen loop_ = &loop; 562745a763SShuo Chen cond_.notify(); 572745a763SShuo Chen } 582745a763SShuo Chen 592745a763SShuo Chen loop.loop(); 602745a763SShuo Chen //assert(exiting_); 612745a763SShuo Chen} 622745a763SShuo Chen 63