-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadPool.cpp
More file actions
41 lines (35 loc) · 992 Bytes
/
threadPool.cpp
File metadata and controls
41 lines (35 loc) · 992 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
#include <assert.h>
#include "common/common.h"
#include "workerThread.h"
#include "task.h"
#include "threadPool.h"
USING_NAMESPACE(std)
NAMESPACE_SETUP(Util)
ThreadPool::ThreadPool(size_t threadNum, size_t bufferQueueSize)
:mWorkerThreadNum(threadNum),
mBufferQueueSize(bufferQueueSize),
mBufferQueuePtr(new BufferQueue(bufferQueueSize)) {
assert(threadNum);
assert(bufferQueueSize);
assert(mBufferQueuePtr);
// init mWorkerThreadVec
mWorkerThreadVec.resize(threadNum);
for (size_t i = 0; i < threadNum; i++) {
mWorkerThreadVec[i].reset(new WorkerThread(mBufferQueuePtr));
}
}
ThreadPool::~ThreadPool() {
for(size_t i = 0; i < mWorkerThreadNum; i++) {
mWorkerThreadVec[i]->Join();
}
}
void ThreadPool::Start() {
for(size_t i = 0; i < mWorkerThreadNum; i++) {
mWorkerThreadVec[i]->Start();
}
}
void ThreadPool::AddTask(Task* task) {
assert(task);
mBufferQueuePtr->Push(task);
}
NAMESPACE_END(Util)