forked from foshougua/network-dash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyThread.cpp
More file actions
71 lines (55 loc) · 1.23 KB
/
MyThread.cpp
File metadata and controls
71 lines (55 loc) · 1.23 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*************************************************************************
> File Name: MyThread.cpp
> Author:
> Mail:
> Created Time: 2017年05月09日 星期二 09时44分02秒
************************************************************************/
#include "MyThread.h"
#include "MyThreadPool.h"
#include <iostream>
using std::cout;
using std::thread;
int MyThread::s_threadnumber = 0;
MyThread::MyThread(MyThreadPool *pool):mythreadpool_(pool), isdetach_(true)
{
//加锁?
s_threadnumber++;
threadid_ = s_threadnumber;
}
void MyThread::setisdetach(bool detach)
{
isdetach_ = detach;
}
void MyThread::Assign(Task *t)
{
task_ =t;
}
void MyThread::Run()
{
cout <<"Thread:"<< threadid_ << " run ";
//执行任务
task_->Run();
//从Busy中移除
this->mythreadpool_->RemoveThreadFromBusy(this);
}
int MyThread::getthreadid()
{
return this->threadid_;
}
void MyThread::StartThread()
{
//设置thread,开始执行
this->thread_ = thread(&MyThread::Run, this);
if (isdetach_ == true)
thread_.detach();
else
thread_.join();
}
bool operator==(MyThread my1, MyThread my2)
{
return my1.threadid_ == my2.threadid_;
}
bool operator!=(MyThread my1, MyThread my2)
{
return !(my1.threadid_ == my2.threadid_);
}