forked from foshougua/network-dash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyThreadPool.cpp
More file actions
105 lines (95 loc) · 2.35 KB
/
MyThreadPool.cpp
File metadata and controls
105 lines (95 loc) · 2.35 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*************************************************************************
> File Name: MyThreadPool.cpp
> Author:
> Mail:
> Created Time: 2017年05月09日 星期二 09时44分44秒
************************************************************************/
#include "MyThreadPool.h"
#include <iostream>
using namespace std;
MyThreadPool::MyThreadPool(int number)
{
issurvive_ = true;
number_of_thread_ = number;
//创建线程 到 空闲容器
idle_thread_container_.assign(number, this);
thread_this_ = thread(&MyThreadPool::Start, this);
thread_this_.detach();
}
MyThreadPool::~MyThreadPool()
{
}
void MyThreadPool::EndMyThreadPool()
{
issurvive_ =false;
}
void MyThreadPool::AddIdleThread(int n)
{
idle_mutex_.lock();
idle_thread_container_.assign(n, this);
number_of_thread_ += n;
idle_mutex_.unlock();
}
void MyThreadPool::Start()
{
cout<<"Main Thread Run"<<endl;
while (true)
{
if (issurvive_==false)
{
//如果线程池结束运行,那么循环等待 工作容器中的 所有线程 结束(size 为0)
busy_mutex_.lock();
if (busy_thread_container_.size()!=0)
{
busy_mutex_.unlock();
continue;
}
busy_mutex_.unlock();
break;
}
idle_mutex_.lock();
if (idle_thread_container_.size() == 0)
{
idle_mutex_.unlock();
continue;
}
idle_mutex_.unlock();
task_mutex_.lock();
if (task_container_.size() == 0)
{
task_mutex_.unlock();
continue;
}
//空闲容器 不为空
//任务容器 不为空
Task *newTask = task_container_.top();
task_container_.pop();
task_mutex_.unlock();
idle_mutex_.lock();
MyThread *topIdleThread = idle_thread_container_.top();
idle_thread_container_.pop();
topIdleThread->Assign(newTask);
idle_mutex_.unlock();
busy_mutex_.lock();
busy_thread_container_.push(topIdleThread);
busy_mutex_.unlock();
topIdleThread->StartThread();
}
}
void MyThreadPool::AddTask(Task *Task, int priority = (PRIORITY::NORMAL))
{
Task->SetPriority(priority);
task_mutex_.lock();
task_container_.push(Task);
task_mutex_.unlock();
}
void MyThreadPool::RemoveThreadFromBusy(MyThread *myThread)
{
busy_mutex_.lock();
cout << "Thread:" << myThread->getthreadid()<< " remove from busylist" << endl;
busy_thread_container_.erase(myThread);
busy_mutex_.unlock();
idle_mutex_.lock();
idle_thread_container_.push(myThread);
idle_mutex_.unlock();
}