当我们需要追求性能时候,一个进程的 创建销毁时间也许不算什么,但是当进程需要调用大量进程时候,但是每一个线程运行时间在创建销毁这个线程当中所占比例并不是百分之百,换句话说线程的创建销毁时间是不可以忽略的
那么类似与懒汉模式当我们需要创建大量线程时候,就提前创建一组线程然后我们用的时候直接在这个线程组里面拿这样不是牛逼很多了嘛!
我们通过一个队列是线程有一个先来后到的顺序,然后在队列中取之运行你要运行的某一个进程函数。
- 还是看代码吧,不知道说什么了~~~
#include <iostream>
#include <queue>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#define MAX_THREAD 5
#define MAX_QUEUE 10
class Task
{
private:
void *_data;
public:
void SetData(void *data)
{
_data = data;
}
bool Run()
{
srand(time(NULL));
int n = rand() % 5;
//任务所要执行的操作
printf("thread:%p--run:%s--sleep:%d\n",
pthread_self(), _data, n);
sleep(n);
return true;
}
};
class ThreadPool
{
private:
int _max_thread;
int _cur_thread;
bool _stop_flags;//线程池中线程的退出标志 1-退出
int _cap;
std::queue<Task *> _tlist;
pthread_mutex_t _lock;
pthread_cond_t _empty;
pthread_cond_t _full;
void ThreadLock()
{
pthread_mutex_lock(&_lock);
}
void ThreadUnLock()
{
pthread_mutex_unlock(&_lock);
}
//任务队列为空则等待
void ConsumerWait()
{
pthread_cond_wait(&_empty, &_lock);
}
void ConsumerNotice()
{
pthread_cond_signal(&_empty);
}
void ConsumerNoticeAll()
{
pthread_cond_broadcast(&_empty);
}
//任务队列满了要等待
void ProductorWait()
{
pthread_cond_wait(&_full, &_lock);
}
void ProductorNotice()
{
pthread_cond_signal(&_full);
}
bool QueueIsEmpty()
{
return _tlist.empty();
}
bool QueueIsFull()
{
return (_tlist.size() == _cap);
}
bool ThreadIsStop()
{
return _stop_flags;
}
void ThreadExit()
{
_cur_thread--;
ThreadUnLock();
ProductorNotice();
pthread_exit(NULL);
}
bool QueuePopData(Task **task)
{
*task = _tlist.front();
_tlist.pop();
return true;
}
static void *thr_start(void *arg)
{
ThreadPool *pool = (ThreadPool*)arg;
while(1) {
printf("into thr_start\n");
pool->ThreadLock();
while(pool->QueueIsEmpty() && !pool->ThreadIsStop()) {
pool->ProductorNotice();
pool->ConsumerWait();
}
if (pool->QueueIsEmpty() && pool->ThreadIsStop()) {
printf("task queue is empty\n");
pool->ThreadExit();
}
printf("get data\n");
Task *task;
pool->QueuePopData(&task);
pool->ThreadUnLock();
task->Run();
}
return NULL;
}
public:
ThreadPool(int max_thread = MAX_THREAD,
int max_queue = MAX_QUEUE):_max_thread(max_thread),
_cur_thread(max_thread),_cap(max_queue),
_stop_flags(false) {
pthread_mutex_init(&_lock, NULL);
pthread_cond_init(&_full, NULL);
pthread_cond_init(&_empty, NULL);
}
~ThreadPool()
{
pthread_mutex_destroy(&_lock);
pthread_cond_destroy(&_full);
pthread_cond_destroy(&_empty);
}
bool Start()
{
int i = 0;
int ret = -1;
pthread_t tid;
for (i = 0; i < _max_thread; i++) {
ret = pthread_create(&tid, NULL, thr_start, (void*)this);
if (ret != 0) {
printf("create thread error\n");
return false;
}
pthread_detach(tid);
}
return true;
}
bool Stop()
{
ThreadLock();
if (_stop_flags == true) {
ThreadUnLock();
return false;
}
_stop_flags = true;
while(_cur_thread > 0) {
ConsumerNoticeAll();
ProductorWait();
}
ThreadUnLock();
return true;
}
bool AddTask(Task *task)
{
ThreadLock();
while(QueueIsFull()) {
ConsumerNotice();
ProductorWait();
}
printf("add data~~\n");
_tlist.push(task);
ThreadUnLock();
ConsumerNotice();
return true;
}
};
int main()
{
ThreadPool t;
t.Start();
int i = 0;
Task task[10];
for (i = 0; i < 10; i++) {
printf("add data\n");
task[i].SetData((void*)"SingHwnag2019!!");
t.AddTask(&task[i]);
}
t.Stop();
return 0;
}
代码上总体来说了 线程池 = 多个线程 + 任务队列