
- 线程池的实现
- 1:自定义封装的条件变量
//condition.h
#ifndef _CONDITION_H_
#define _CONDITION_H_ #include <pthread.h> typedef struct condition
{
pthread_mutex_t pmutex;
pthread_cond_t pcond;
}condition_t; int condition_init(condition_t *cond);
int condition_lock(condition_t *cond);
int condition_unlock(condition_t *cond);
int condition_wait(condition_t *cond);
int condition_timewait(condition_t *cond,const struct timespec *abstime);
int condition_signal(condition_t *cond);
int condition_broadcast(condition_t *cond);
int condition_destroy(condition_t *cond); #endif//condition.c
#include "condition.h" int condition_init(condition_t *cond)
{
int status;
if((status = pthread_mutex_init(&cond->pmutex,NULL)))
return status;
if((status = pthread_cond_init(&cond->pcond,NULL)))
return status;
return ;
} int condition_lock(condition_t *cond)
{
return pthread_mutex_lock(&cond->pmutex);
}
int condition_unlock(condition_t *cond)
{
return pthread_mutex_unlock(&cond->pmutex);
}
int condition_wait(condition_t *cond)
{
return pthread_cond_wait(&cond->pcond,&cond->pmutex);
}
int condition_timewait(condition_t *cond,const struct timespec *abstime)
{
return pthread_cond_timedwait(&cond->pcond,&cond->pmutex,abstime);
}
int condition_signal(condition_t *cond)
{
return pthread_cond_signal(&cond->pcond);
}
int condition_broadcast(condition_t *cond)
{
return pthread_cond_broadcast(&cond->pcond);
}
int condition_destroy(condition_t *cond)
{
int status;
if((status = pthread_mutex_destroy(&cond->pmutex)))
return status;
if((status = pthread_cond_destroy(&cond->pcond)))
return status;
return ; }
- 2:线程池逻辑
//threadpool.h
#include "condition.h" typedef struct task
{
void *(*run)(void *arg);
void *arg;
struct task *next;
}task_t; typedef struct threadpool
{
condition_t ready;
task_t *first;
task_t *last;
int counter;
int idle;
int max_threads;
int quit;
}threadpool_t; void threadpool_init(threadpool_t *pool, int threads); void threadpool_add_task(threadpool_t *pool,void *(*run)(void *arg),void *arg); void threadpool_destroy(threadpool_t *pool);//threadpool.c
#include "threadpool.h"
#include <string.h>
#include <errno.h>
#include <time.h> void *thread_routine(void *arg)
{
printf("thread 0x%0x is starting\n",(int)pthread_self());
struct timespec abstime;
int timeout; threadpool_t *pool = (threadpool_t*)arg;
while()
{
timeout = ;
condition_lock(&pool->ready);
pool->idle++;
while(pool->first == NULL && pool->quit)
{
// condition_wait(&pool->ready);
clock_gettime(CLOCK_REALTIME, &abstime);
abstime.tv_sec += ;
int status = condition_timewait(&pool->ready,&abstime);
if(status == ETIMEDOUT)
{
printf("thread time out\n");
timeout = ;
break;
}
} pool->idle--;
if(pool->first != NULL)
{
task_t *t = pool->first;
pool->first = t->next;
//由于执行下线程工作需要时间,所以先解锁以让其他线程能过获取资源
condition_unlock(&pool->ready);
t->run(t->arg);
free(t);
condition_lock(&pool->ready);
} if(pool->quit && pool->first == NULL)
{
pool->counter--;
if(pool->counter == )
{
condition_signal(&pool->ready);
}
condition_unlock(&pool->ready);
break;
} if(timeout && pool->first == NULL)
{
pool->counter--;
condition_unlock(&pool->ready);
break;
}
condition_unlock(&pool->ready);
} printf("thead 0x%0x is exting\n",(int)pthread_self());
return NULL;
}
void threadpool_init(threadpool_t *pool, int threads)
{
// condition_t ready;
// task_t *first;
// task_t *last;
// int counter;
// int idle;
// int max_threads;
// int quit; condition_init(&pool->ready);
pool->first = NULL;
pool->last = NULL;
pool->counter= ;
pool->idle = ;
pool->max_threads = threads;
pool->quit = ;
} void threadpool_add_task(threadpool_t *pool,void *(*run)(void *arg),void *arg)
{
task_t* newtask = malloc(sizeof(task_t));
newtask->run = run;
newtask->arg = arg;
newtask->next = NULL; condition_lock(&pool->ready); //add task to tasklist tail
if(pool->first ==NULL)
pool->first = newtask;
else
pool->last->next = newtask;
pool->last = newtask; // if have wait thread,wake to do work
if(pool->idle > )
{
condition_signal(&pool->ready);
}
else if(pool->counter < pool->max_threads)
{
pthread_t tid;
pthread_create(&tid, NULL, thread_routine, pool);
pool->counter++;
} condition_unlock(&pool->ready);
} void threadpool_destroy(threadpool_t *pool)
{
if(pool->quit)
{
return;
}
condition_lock(&pool->ready);
pool->quit = ;
if(pool->counter > )
{
if(pool->idle > )
{
condition_broadcast(&pool->ready);
} while(pool->counter > )
{
condition_wait(&pool->ready);
}
}
condition_unlock(&pool->ready);
condition_destroy(&pool->ready);
}
-
3:main
#include "threadpool.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h> void* mytask(void* arg)
{
printf("thread 0x%0x is working on task %d\n",(int)pthread_self(),*(int*)arg);
sleep();
free(arg);
return NULL;
} int main(void)
{
threadpool_t pool;
threadpool_init(&pool,); int i;
for(i = ; i < ; i++)
{
int *arg = (int*)malloc(sizeof(int));
*arg = i;
threadpool_add_task(&pool,mytask,arg);
}
// sleep(15);
threadpool_destroy(&pool);
return ;
}
- 1:自定义封装的条件变量