-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventLoopThread.cpp
More file actions
43 lines (35 loc) · 854 Bytes
/
EventLoopThread.cpp
File metadata and controls
43 lines (35 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
// Created by parallels on 2020/1/20.
//
#include <unistd.h>
#include "EventLoopThread.h"
EventLoopThread::EventLoopThread() :
loop_(NULL),
thread_(std::bind(&EventLoopThread::run, this), "EventLoopThread"),
mutex_(),
cond_(mutex_),
flag_(false)
{}
EventLoopThread::~EventLoopThread() {}
void EventLoopThread::run() {
mutex_.lock();
while(!flag_);
usleep(100);
cond_.notify();
mutex_.unlock();
while(loop_ == NULL);
loop_->loop();
}
EventLoop *EventLoopThread::startLoop() {
EventLoop *loop = new EventLoop();
loop_ = loop;
loop_->epoll_->addEpoll(loop_->wait_Task_);
mutex_.lock();
flag_ = false;
thread_.start();
flag_ = true;
cond_.wait();
usleep(100000);//需要等到进入到loop的循环,startLoop才能返回
mutex_.unlock();
return loop_;
}