// Copyright 2019, Chen Shuaihao.
//
//Author: Chen Shuaihao
//
//
#include "EventLoopThread.h"
#include
#include
//#include
//#include
//#include
EventLoopThread::EventLoopThread()
: th_(),
threadid_(-1),
threadname_("IO thread "),
loop_(NULL)
{
}
EventLoopThread::~EventLoopThread()
{
//线ç¨ç»ææ¶æ¸
ç
std::cout << "Clean up the EventLoopThread id: " << std::this_thread::get_id() << std::endl;
loop_->Quit();//忢IO线ç¨è¿è¡
th_.join();//æ¸
çIO线ç¨ï¼é²æ¢å
åæ³æ¼ï¼å 为pthread_createdåcalloc
}
EventLoop* EventLoopThread::GetLoop()
{
return loop_;
}
void EventLoopThread::Start()
{
//create thread
th_ = std::thread(&EventLoopThread::ThreadFunc, this);
}
void EventLoopThread::ThreadFunc()
{
EventLoop loop;
loop_ = &loop;
threadid_ = std::this_thread::get_id();
std::stringstream sin;
sin << threadid_;
threadname_ += sin.str();
std::cout << "in the thread:" << threadname_ << std::endl;
try
{
//std::cout << "EventLoopThread::ThreadFunc " << std::endl;
loop_->loop();
}
catch (std::bad_alloc& ba)
{
std::cerr << "bad_alloc caught in EventLoopThread::ThreadFunc loop: " << ba.what() << '\n';
}
//loop_->loop();
}