-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thread.h
45 lines (40 loc) · 998 Bytes
/
Thread.h
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
44
45
//=====================================================================
//
// Thread.h -
//
// Created by wwq on 2022/08/21
// Last Modified: 2022/08/21 15:27:17
//
//=====================================================================
#ifndef SIMPLE_MUDUO_THREAD_H
#define SIMPLE_MUDUO_THREAD_H
#include <functional>
#include <string>
#include <memory>
#include <atomic>
#include <thread>
#include "noncopyable.h"
namespace simple_muduo {
class Thread : noncopyable
{
public:
using ThreadFunc = std::function<void()>;
explicit Thread(ThreadFunc, const std::string name = std::string());
~Thread();
void start();
void join();
bool started() {return started_;}
pid_t tid() const {return tid_;}
private:
void setDefaultName();
private:
bool started_;
bool joined_;
std::shared_ptr<std::thread> thread_;
pid_t tid_; // 在线程创建时再绑定
ThreadFunc func_; // 线程回调函数
std::string name_;
static std::atomic_int numCreated_;
};
}
#endif