-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventLoopThread.h
More file actions
43 lines (28 loc) · 1.11 KB
/
Copy pathEventLoopThread.h
File metadata and controls
43 lines (28 loc) · 1.11 KB
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
#ifndef MYMUDUO_EVENTLOOPTHREAD_H
#define MYMUDUO_EVENTLOOPTHREAD_H
#include "Thread.h"
#include <functional>
#include <condition_variable>
#include <mutex>
class EventLoop;
// IO线程
//
// 通过包含一个Thread类使其成为一个线程,与此同时实现自己的threadFunc使其成为一个具有自定义功能的线程,这里是IO线程。
//
class EventLoopThread {
public:
using ThreadInitCallback = std::function<void(EventLoop*)>;
EventLoopThread(const ThreadInitCallback& cb,
const std::string& name);
~EventLoopThread();
EventLoop* startLoop(); // 启动EventLoop,并返回对应的指针
private:
void threadFunc(); // 线程需要运行的函数
EventLoop* loop_; // 线程运行的EventLoop
Thread thread_; // 线程
// 为什么这里又不用unique_ptr了?
std::mutex mtx_; // 锁
std::condition_variable cond_; // 条件变量
ThreadInitCallback callback_; // 每个线程执行前的初始化操作,由上层设置(这里是EventLoopThreadPool)
};
#endif // MYMUDUO_EVENTLOOPTHREAD_H