-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathThread.cpp
More file actions
56 lines (49 loc) · 867 Bytes
/
Copy pathThread.cpp
File metadata and controls
56 lines (49 loc) · 867 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
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include "Thread.h"
using namespace xnet;
std::atomic<int> Thread::numCreated_ = 0;
Thread::Thread(const ThreadFunc& func, const std::string& name):
func_(func),
name_(name),
started_(false),
joined_(false)
{
numCreated_++;
if (name_.empty())
{
char buf[32];
_snprintf_s(buf, sizeof buf, "Thread%d", numCreated_);
name_ = buf;
}
}
Thread::Thread(ThreadFunc&& func, const std::string& name):
func_(std::move(func)),
name_(name),
started_(false),
joined_(false)
{
numCreated_++;
if (name_.empty())
{
char buf[32];
_snprintf_s(buf, sizeof buf, "Thread%d", numCreated_);
name_ = buf;
}
}
Thread::~Thread()
{
if (started_ && !joined_)
{
pThread_->detach();
}
}
void Thread::start()
{
started_ = true;
pThread_ = std::make_shared<std::thread>(func_);
}
void Thread::join()
{
joined_ = true;
pThread_->join();
}