/
ThreadPool.cpp
56 lines (54 loc) · 1.14 KB
/
ThreadPool.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
/*********************************************
> File : ThreadPool.cpp
> Author: NewYork
> Mail : 2287794993@qq.com
> Time : Fri 03 Jul 2015 10:13:17 AM CST
********************************************/
#include<iostream>
#include<vector>
#include "MyPoolThread.h"
using namespace std;
namespace wd
{
void ThreadPool::start()
{
for(std::size_t index=0; index!=numThreads_; index++)
{
Thread* pThread = new MyPoolThread(*this);
vecThreads_.push_back(pThread);
pThread->start();
}
}
ThreadPool::~ThreadPool()
{
if(!isExit_)
{
isExit_ = true;
for(auto it: vecThreads_)
{
(*it)->join();
delete *it;
}
}
vecThreads_.clear();
}
void ThreadPool::addTask(Task task)
{
buffer_.push(task);
}
Task ThreadPool::getTask()
{
return buffer_.pop();
}
void ThreadPool::runInThread()
{
while(!isExit_)
{
Task task =getTask()
if(task!=NULL)
{
task->process();
}
}
}
}