-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCThreadPool.cpp
More file actions
146 lines (120 loc) · 2.89 KB
/
CThreadPool.cpp
File metadata and controls
146 lines (120 loc) · 2.89 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: CThreadPool.cpp
* Author: hxw
*
* Created on September 18, 2017, 4:59 PM
*/
#include <algorithm>
#include <iostream>
#include "CThreadPool.h"
#include "CThread.h"
CThreadPool_t::CThreadPool_t() :
threads_(NULL)
, task_(NULL)
, thread_count_(0)
, queue_size_(0)
, task_count_(0)
, head_(0)
, tail_(0)
, started_(false)
, shudown_(false) {
}
int CThreadPool_t::start(int thread_count, int queue_size) {
std::lock_guard<decltype(lock_) > l(lock_);
if (started_) {
return -1;
}
queue_size_ = queue_size;
threads_ = new CThread_t[thread_count];
for (int i = 0; i < thread_count; ++i) {
threads_[i].set_thread_pool(this);
auto ret = threads_[i].start();
if (0 != ret) {
std::cout << "线程启动失败." << std::endl;
destroy();
return -1;
}
++thread_count_;
}
task_ = new Task_t[queue_size];
started_ = true;
return 0;
}
int CThreadPool_t::destroy() {
lock_.lock();
if (shudown_) {
lock_.unlock();
return -1;
}
shudown_ = true;
started_ = false;
notify_.notify_all();
lock_.unlock();
for (int i = 0; i < thread_count_; ++i) {
threads_[i].join();
}
if (threads_) {
delete[] threads_;
threads_ = NULL;
}
while (this->task_count_ != 0) {
Task_t task = task_[head_];
head_ = (head_ + 1) % queue_size_;
--task_count_;
if (task.destroy_func) {
task.destroy_func(task.argument);
}
}
if (task_) {
delete[] task_;
task_ = NULL;
}
return 0;
}
int CThreadPool_t::add(callback_t func, void *arg) {
return add(func, NULL, arg);
}
int CThreadPool_t::add(callback_t call_func, callback_t destroy_func, void *arg) {
if (call_func == NULL) {
return -1;
}
std::lock_guard<decltype(lock_) > l(lock_);
if (shudown_) {
return -1;
}
if (task_count_ == queue_size_) {
return -1;
}
task_[tail_].function = call_func;
task_[tail_].destroy_func = destroy_func;
task_[tail_].argument = arg;
tail_ = (tail_ + 1) % queue_size_;
++task_count_;
notify_.notify_one();
return 0;
}
Task_t CThreadPool_t::get() {
// std::lock_guard<decltype(lock_) > l(lock_);
std::unique_lock<decltype(lock_) > ul(lock_);
Task_t task;
task.argument = NULL;
task.function = NULL;
while (true) {
while ((!shudown_) && task_count_ == 0) {
notify_.wait(ul);
}
if (shudown_) {
return task;
}
task = task_[head_];
head_ = (head_ + 1) % queue_size_;
--task_count_;
break;
}
return task;
}