-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadpool.cpp
50 lines (44 loc) · 1.47 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
#include "common.h"
#include "threadpool.h"
#include "task.h"
#include "taskqueue.h"
#include "taskThreadManager.h"
ThreadPool::ThreadPool(TaskThreadManager* pManager_): pManager(pManager_)
{
}
void ThreadPool::createThreads()
{
for(int i=0; i< max_thread_num; i++)
{
thread_pool.push_back(new thread(std::bind(&ThreadPool::ThreadProc, this, std::placeholders::_1),this));
//thread_pool.push_back(new thread(&ThreadPool::ThreadProc, this,this));
}
}
void ThreadPool::ThreadProc(void* param)
{
//这里延时1s是让task准备好
sleep(1);
ThreadPool* pPool= (ThreadPool*)(param);
TaskThreadManager* pManager = pPool->pManager;
while(1)
{
unique_lock<mutex> lock(pManager->task_thread_mutex);
while(pManager->pTask_queue->size()==0)
{
pManager->task_thread_condition.wait(lock);
}
cout<<"ThreadProc队列尺寸为"<<pManager->pTask_queue->size()<<" "<<"头部元素:"<<*(int*)(pManager->pTask_queue->task_queue.front()->param);
shared_ptr<Task> pTask = pManager->pTask_queue->pop();
cout<<"ThreadProc取出task成功,剩余size为"<<pManager->pTask_queue->size()<<endl;
lock.unlock();
cout<<"ThreadProc线程id为:"<<std::this_thread::get_id()<<endl;
pTask->run();
}
}
void ThreadPool::join()
{
for(int i=0; i< max_thread_num; i++)
{
thread_pool[i]->join();
}
}