-
Notifications
You must be signed in to change notification settings - Fork 1
/
Thread.h
38 lines (29 loc) · 819 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
#pragma once
#include "noncopyable.h"
#include <atomic>
#include <functional>
#include <memory>
#include <string>
#include <thread>
#include <unistd.h>
class Thread : noncopyable {
public:
using ThreadFunc = std::function<void()>;
explicit Thread(ThreadFunc func, const std::string &name = std::string());
~Thread();
void start();
void join();
bool started() const { return started_; }
pid_t tid() const { return tid_; }
const std::string &name() const { return name_; }
static int numCreated() { return numCreated_; }
private:
void setDefaultName();
bool started_;
bool joined_;
std::shared_ptr<std::thread> thread_; // 控制 thread_ 运行时机
pid_t tid_;
ThreadFunc func_;
std::string name_;
static std::atomic_int numCreated_;
};