main.c
void *mytask(void *arg1, void *arg2)
{
long n = (long)arg1;
printf("第二個參數是 is %s\n", (char *)arg2);
printf("執行緒id爲[%ld]的執行緒準備工作 %ld 秒...\n",
pthread_self(), n);
sleep(n);
printf("執行緒id爲[%ld]的執行緒工作 %ld 秒結束了******************\n",
pthread_self(), n);
return NULL;
}
void *count_time(void *arg)
{
int i = 0;
while(1)
{
sleep(1);
printf("當前是第%d秒\n", ++i);
}
}
int main(void)
{
pthread_t a;
//只是用來建立一天用於計時的執行緒,跟執行緒池一點關係都沒有
pthread_create(&a, NULL, count_time, NULL);
thread_pool *pool = malloc(sizeof(thread_pool));
// 1, 初始化執行緒池,並且一開始就有兩條執行緒
init_pool(pool, 2);
// 2, 任務投放
printf("投放3個任務\n");
add_task(pool, mytask, (void *)((rand()%10)*1L), (void *)("hhhhh"));
add_task(pool, mytask, (void *)((rand()%10)*1L), (void *)("xixixi"));
add_task(pool, mytask, (void *)((rand()%10)*1L), (void *)("heiheihei"));
// 3, 檢查當前執行緒數量
printf("當前執行緒數量爲:%d\n",
remove_thread(pool, 0));
sleep(9);
//增加執行緒數量
add_thread(pool, 2);
sleep(5);
// 6, 刪除執行緒(隨意,因爲刪除執行緒池的時候,會全部刪除執行緒)
printf("刪除3條執行緒,當前執行緒數還剩: %d\n",
remove_thread(pool, 3));
// 7, 刪除執行緒池
destroy_pool(pool);
//8、釋放執行緒池資源
free(pool);
return 0;
}
thread_pool.c
#include "thread_pool.h"
void handler(void *arg)
{
pthread_mutex_unlock((pthread_mutex_t *)arg);
}
void *routine(void *arg)
{
thread_pool *pool = (thread_pool *)arg;
struct task *p;
while(1)
{
pthread_cleanup_push(handler, (void *)&pool->lock);
pthread_mutex_lock(&pool->lock);
while(pool->waiting_tasks == 0 && !pool->shutdown)
{
pthread_cond_wait(&pool->cond, &pool->lock);
}
if(pool->waiting_tasks == 0 && pool->shutdown == true)
{
pthread_mutex_unlock(&pool->lock);
pthread_exit(NULL);
}
p = pool->task_list->next;
pool->task_list->next = p->next;
pool->waiting_tasks--;
pthread_mutex_unlock(&pool->lock);
pthread_cleanup_pop(0);
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
(p->task)(p->myarg1, p->myarg2);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
free(p);
}
pthread_exit(NULL);
}
bool init_pool(thread_pool *pool, unsigned int threads_number)
{
pthread_mutex_init(&pool->lock, NULL);
pthread_cond_init(&pool->cond, NULL);
pool->shutdown = false;
pool->task_list = malloc(sizeof(struct task));
pool->tids = malloc(sizeof(pthread_t) * MAX_ACTIVE_THREADS);
if(pool->task_list == NULL || pool->tids == NULL)
{
perror("allocate memory error");
return false;
}
pool->task_list->next = NULL;
pool->waiting_tasks = 0;
pool->active_threads = threads_number;
int i;
for(i=0; i<pool->active_threads; i++)
{
if(pthread_create(&((pool->tids)[i]), NULL,
routine, (void *)pool) != 0)
{
perror("create threads error");
return false;
}
}
return true;
}
bool add_task(thread_pool *pool,
void *(*fun_name)(void *myarg1, void *myarg2), void *arg1, void *arg2)
{
struct task *new_task = malloc(sizeof(struct task));
if(new_task == NULL)
{
perror("allocate memory error");
return false;
}
new_task->task = fun_name;
new_task->myarg1 = arg1;
new_task->myarg2 = arg2;
new_task->next = NULL;
pthread_mutex_lock(&pool->lock);
if(pool->waiting_tasks >= MAX_WAITING_TASKS)
{
pthread_mutex_unlock(&pool->lock);
fprintf(stderr, "too many tasks.\n");
free(new_task);
return false;
}
struct task *tmp = pool->task_list;
while(tmp->next != NULL)
tmp = tmp->next;
tmp->next = new_task;
pool->waiting_tasks++;
pthread_mutex_unlock(&pool->lock);
pthread_cond_signal(&pool->cond);
return true;
}
int add_thread(thread_pool *pool, unsigned additional_threads)
{
if(additional_threads == 0)
return 0;
unsigned total_threads =
pool->active_threads + additional_threads;
int i, actual_increment = 0;
for(i = pool->active_threads;
i < total_threads && i < MAX_ACTIVE_THREADS;
i++)
{
if(pthread_create(&((pool->tids)[i]),
NULL, routine, (void *)pool) != 0)
{
perror("add threads error");
if(actual_increment == 0)
return -1;
break;
}
actual_increment++;
}
pool->active_threads += actual_increment;
return actual_increment;
}
int remove_thread(thread_pool *pool, unsigned int removing_threads)
{
if(removing_threads == 0)
return pool->active_threads;
int remain_threads = pool->active_threads - removing_threads;
remain_threads = remain_threads>0 ? remain_threads:1;
int i;
for(i=pool->active_threads-1; i>remain_threads-1; i--)
{
errno = pthread_cancel(pool->tids[i]);
if(errno != 0)
break;
}
if(i == pool->active_threads-1)
return -1;
else
pool->active_threads = i+1;
return pool->active_threads;
}
bool destroy_pool(thread_pool *pool)
{
pool->shutdown = true;
pthread_cond_broadcast(&pool->cond);
int i;
for(i=0; i<pool->active_threads; i++)
{
errno = pthread_join(pool->tids[i], NULL);
if(errno != 0)
{
printf("join tids[%d] error: %s\n",
i, strerror(errno));
}
else
printf("[%u] is joined\n", (unsigned)pool->tids[i]);
}
free(pool->task_list);
free(pool->tids);
return true;
}
thread_pool.h
#ifndef _THREAD_POOL_H_
#define _THREAD_POOL_H_
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <pthread.h>
#define MAX_WAITING_TASKS 1000
#define MAX_ACTIVE_THREADS 20
struct task
{
void *(*task)(void *arg1,void *arg2);
void *myarg1;
void *myarg2;
struct task *next;
};
typedef struct thread_pool
{
pthread_mutex_t lock;
pthread_cond_t cond;
struct task *task_list;
pthread_t *tids;
unsigned waiting_tasks;
unsigned active_threads;
bool shutdown;
}thread_pool;
bool
init_pool(thread_pool *pool,
unsigned int threads_number);
bool
add_task(thread_pool *pool,
void *(*fun_name)(void *myarg1, void *myarg2),
void *arg1, void *arg2);
int
add_thread(thread_pool *pool,
unsigned int additional_threads_number);
int
remove_thread(thread_pool *pool,
unsigned int removing_threads_number);
bool destroy_pool(thread_pool *pool);
void *routine(void *arg);
#endif
Makefile
CC = gcc
CFLAGS = -O0 -Wall -g -lpthread
test:main.c thread_pool.c
$(CC) $^ -o $@ $(CFLAGS)
debug:main.c thread_pool.c
$(CC) $^ -o $@ $(CFLAGS) -DDEBUG
clean:
$(RM) .*.sw? test debug *.o
.PHONY:all clean
文章來源:微信公衆號【圖控大叔】