main.cpp
printf("thread %ld is working, number = %d\n", pthread_self(), num);
printf("%s 向你问好!\n", "threadpool");
ThreadPool* pool = threadPoolCreate(3, 10, 100);
for (int i = 0; i < 100; i++)
int* num = (int*)malloc(sizeof(int));
threadPoolAdd(pool, taskFunc, num);
threadpool.h
typedef struct ThreadPool ThreadPool;
ThreadPool* threadPoolCreate(int min, int max, int queueSize);
int threadPoolDestory(ThreadPool* pool);
void threadPoolAdd(ThreadPool* pool, void(*func)(void*), void* arg);
int threadPoolBusyNum(ThreadPool* pool);
int threadPoolAliveNum(ThreadPool* pool);
void* manager(void* arg);
void threadExit(ThreadPool* pool);
threadpool.cpp
void (*function)(void* arg);
pthread_mutex_t mutexPool;
pthread_mutex_t mutexBusy;
ThreadPool* threadPoolCreate(int min, int max, int queueSize)
ThreadPool* pool = (ThreadPool*)malloc(sizeof(ThreadPool));
printf("malloc threadpool fail .. \n");
pool->threadIDs = (pthread_t*)malloc(sizeof(pthread_t) * max);
if (pool->threadIDs == NULL)
printf("malloc threadIDs fail .. \n");
memset(pool->threadIDs, 0, sizeof(pthread_t) * max);
if (pthread_mutex_init(&pool->mutexPool, NULL) != 0 ||
pthread_mutex_init(&pool->mutexBusy, NULL) != 0 ||
pthread_cond_init(&pool->notEmpty, NULL) != 0 ||
pthread_cond_init(&pool->notFull, NULL) != 0)
printf("mutex init fail ... \n");
pool->taskQ = (Task*)malloc(sizeof(Task) * queueSize);
pool->queueCapacity = queueSize;
pthread_create(&pool->managerId, NULL, manager, pool);
for (int i = 0; i < min; i++)
pthread_create(&pool->threadIDs[i], NULL, worker, pool);
if (pool && pool->threadIDs)
int threadPoolDestory(ThreadPool* pool)
for (int i = 0; i < pool->liveNum; i++)
pthread_cond_signal(&pool->notEmpty);
pthread_join(pool->managerId, NULL);
pthread_mutex_destroy(&pool->mutexPool);
pthread_mutex_destroy(&pool->mutexBusy);
pthread_cond_destroy(&pool->notEmpty);
pthread_cond_destroy(&pool->notFull);
void threadPoolAdd(ThreadPool* pool, void(*func)(void*), void* arg)
pthread_mutex_lock(&pool->mutexPool);
while (pool->queueSize==pool->queueCapacity && !pool->shutdown)
pthread_cond_wait(&pool->notFull, &pool->mutexPool);
pthread_mutex_unlock(&pool->mutexPool);
pool->taskQ[pool->queueRear].function = func;
pool->taskQ[pool->queueRear].arg = arg;
pool->queueRear = (pool->queueRear + 1) % pool->queueCapacity;
pthread_cond_signal(&pool->notEmpty);
pthread_mutex_unlock(&pool->mutexPool);
int threadPoolBusyNum(ThreadPool* pool)
pthread_mutex_lock(&pool->mutexBusy);
int busyNum = pool->busyNum;
pthread_mutex_unlock(&pool->mutexBusy);
int threadPoolAliveNum(ThreadPool* pool)
pthread_mutex_lock(&pool->mutexPool);
int aliveNum = pool->liveNum;
pthread_mutex_unlock(&pool->mutexPool);
ThreadPool* pool = (ThreadPool*)arg;
pthread_mutex_lock(&pool->mutexPool);
while (pool->queueSize == 0 && !pool->shutdown)
pthread_cond_wait(&pool->notEmpty, &pool->mutexPool);
if (pool->liveNum > pool->minNum)
pthread_mutex_unlock(&pool->mutexPool);
pthread_mutex_unlock(&pool->mutexPool);
task.function = pool->taskQ[pool->queueFront].function;
task.arg = pool->taskQ[pool->queueFront].arg;
pool->queueFront = (pool->queueFront + 1) % pool->queueCapacity;
pthread_cond_signal(&pool->notFull);
pthread_mutex_unlock(&pool->mutexPool);
printf("thread %ld start working ... \n", pthread_self());
pthread_mutex_lock(&pool->mutexBusy);
pthread_mutex_unlock(&pool->mutexBusy);
printf("thread %ld end working ... \n", pthread_self());
pthread_mutex_lock(&pool->mutexBusy);
pthread_mutex_unlock(&pool->mutexBusy);
ThreadPool* pool = (ThreadPool*)arg;
pthread_mutex_lock(&pool->mutexPool);
int queueSize = pool->queueSize;
int liveNum = pool->liveNum;
pthread_mutex_unlock(&pool->mutexPool);
pthread_mutex_lock(&pool->mutexBusy);
int busyNum = pool->busyNum;
pthread_mutex_unlock(&pool->mutexBusy);
if (queueSize > liveNum && liveNum < pool->maxNum)
pthread_mutex_lock(&pool->mutexPool);
for (
int i =
0; i < pool->maxNum && counter
liveNummaxNum; i++) if (pool->threadIDs[i] == 0) {
pthread_create(&pool->threadIDs[i], NULL, worker, pool);
pthread_mutex_unlock(&pool->mutexPool);
if (busyNum*2 < liveNum && liveNum > pool->minNum)
pthread_mutex_lock(&pool->mutexPool);
pthread_mutex_unlock(&pool->mutexPool);
for (int i = 0; i < NUMBER; i++)
pthread_cond_signal(&pool->notEmpty);
void threadExit(ThreadPool* pool)
pthread_t tid = pthread_self();
for (int i = 0; i < pool->maxNum; i++)
if (pool->threadIDs[i] == tid)
printf("threadExit called, %ld exiting ...\n", tid);