-
Notifications
You must be signed in to change notification settings - Fork 5
/
Thread.cpp
91 lines (80 loc) · 2.62 KB
/
Thread.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*************************************************************************
> File Name: Thread.cpp
> Author: zhangfeng
> Mail: [email protected]
> Created Time: Wed 16 Oct 2019 12:51:29 PM CST
> Target:
************************************************************************/
#include "Thread.h"
#include "ThreadPool.h"
using namespace std;
struct ThreadData {
bool *running_;
bool *shutDown_;
bool *joined_;
std::shared_ptr<ThreadPool> threadHolder_;
pthread_cond_t *cond_;
pthread_mutex_t *mutex_;
std::shared_ptr<ThreadTask> threadTask_;
ThreadData(bool *running, bool *shutDown, bool *joined, std::shared_ptr<ThreadPool> threadHolder, pthread_cond_t *cond, pthread_mutex_t *mutex) :
running_(running),
shutDown_(shutDown),
joined_(joined),
threadHolder_(threadHolder),
cond_(cond),
mutex_(mutex),
threadTask_(nullptr)
{
}
};
void *callBack(void *args) {
ThreadData *threadData = static_cast<ThreadData*>(args);
while(true) {
*threadData->joined_ = true;
*threadData->running_ = false;
while(*threadData->joined_ && threadData->threadHolder_->TaskSize() == 0) {
pthread_cond_wait(threadData->cond_, threadData->mutex_);
}
if(*threadData->shutDown_) {
break;
}
threadData->threadTask_ = threadData->threadHolder_->GetThreadTask();
if(threadData->threadTask_ != nullptr) {
threadData->threadHolder_->IncBusy();
*threadData->running_ = true;
*threadData->joined_ = false;
#if DEBUG
std::cout << pthread_self() << " ready\nbusy: " << threadData->threadHolder_->GetBusy() << "\ntask: " << threadData->threadHolder_->TaskSize()<< std::endl;
#endif
threadData->threadTask_->taskFunc_(threadData->threadTask_->taskArgs_);
threadData->threadHolder_->DscBusy();
}
threadData->threadTask_ = nullptr;
}
}
Thread::Thread(pthread_cond_t *cond, pthread_mutex_t *mutex, std::shared_ptr<ThreadPool> threadHolder) :
cond_(cond),
mutex_(mutex),
threadHolder_(threadHolder),
running_(false),
shutDown_(false)
{
}
Thread::~Thread() {
cond_ = nullptr;
}
void Thread::Start() {
ThreadData *threadData = new ThreadData(&running_, &shutDown_, &joined_, threadHolder_, cond_, mutex_);
if(pthread_create(&pid_, nullptr, callBack, threadData)) {
std::cerr << __FILE__ << __LINE__ << ": create thread falied" << std::endl;
exit(1);
}
}
void Thread::Shut() {
running_ = false;
joined_ = false;
shutDown_ = true;
}
bool Thread::IsAlive() {
return running_;
}