流程
初始化线程池
向线程池添加任务
销毁线程池
线程池初始化
初始化任务队列和工作线程组
将等候在条件变量(任务队列上有任务)上的一个线程唤醒并从该任务队列中取出第一个任务给该线程执行
等待任务队列中所有任务执行完毕
分文件
//task_queue.h
#ifndef __TASK_QUEUE_H__
#define __TASK_QUEUE_H__
#define WORK_THREAD_COUNT 6
typedef struct Task{
void (*pfunc)();// 数据
struct Task* next; // 指针域
}Task;
typedef struct{
Task* head;
Task* tail;
}TaskQueue;
void taskqueue_init(TaskQueue* queue);
void taskqueue_destory(TaskQueue* queue);
void taskqueue_push(TaskQueue* queue,Task* task);
void taskqueue_pop(TaskQueue* queue);
Task* taskqueue_front(TaskQueue* queue);
bool taskqueue_empty(TaskQueue* queue);
#endif //__TASK_QUEUE_H__
//task_queue.c
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "task_queue.h"
void taskqueue_init(TaskQueue* queue){
queue->head = queue->tail = NULL;
}
void taskqueue_destory(TaskQueue* queue){
Task* p = queue->head;
while(p != NULL){
Task* q = p->next;
free(p);
p = q;
}
queue->head = queue->tail = NULL;
}
void taskqueue_push(TaskQueue* queue,Task* task){
Task* copy = (Task*)malloc(sizeof(Task));
memcpy(copy,task,sizeof(Task));
if(NULL == queue->head){ // 入对第一个元素
printf("入队第一个元素:%p\n",copy);
queue->head = copy;
queue->tail = copy;
}else{ // 入队其他元素
printf("入队元素:%p\n",copy);
queue->tail->next = copy;
queue->tail = copy;
}
}
void taskqueue_pop(TaskQueue* queue){
if(queue->head == NULL) return;
Task* p = queue->head;
printf("出队元素:%p\n",p);
queue->head = p->next;
free(p);
}
Task* taskqueue_front(TaskQueue* queue){
return queue->head;
}
bool taskqueue_empty(TaskQueue* queue){
return queue->head == NULL;
}
//thread_pool.h
#ifndef __THREAD_POOL_H__
#define __THREAD_POOL_H__
#include "task_queue.h"
#define WORK_THREAD_COUNT 6
typedef struct{
pthread_t tids[WORK_THREAD_COUNT];///< 工作线程组
TaskQueue queue; ///< 任务队列
pthread_mutex_t mutex;// 互斥量
pthread_cond_t cond;// 条件变量
bool running;
}ThreadPool;
void* func(void* arg);
bool threadpool_init(ThreadPool* pool);
bool threadpool_addtask(ThreadPool* pool,Task* task);
bool threadpool_destroy(ThreadPool* pool);
#endif // __THREAD_POOL_H__
//thread_pool.c
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "thread_pool.h"
void* func(void* arg){
ThreadPool* pool = (ThreadPool*)arg;
pthread_mutex_lock(&pool->mutex);
// 如果没有任务
while(taskqueue_empty(&pool->queue)){
printf("TID:%ld wait\n",pthread_self());
pthread_cond_wait(&pool->cond,&pool->mutex);// 等待任务
if(!pool->running){
pthread_mutex_unlock(&pool->mutex);
pthread_exit(0);
}
}
// 取任务
printf("TID:%ld get task\n",pthread_self());
Task* task = taskqueue_front(&pool->queue);
void(*pfunc)() = task->pfunc;
taskqueue_pop(&pool->queue);
pthread_mutex_unlock(&pool->mutex);
pfunc();// 做任务
}
/**
* @brief 初始化线程池
*
* @param pool 线程池
* @return 初始化结果
*/
bool threadpool_init(ThreadPool* pool){
pool->running = true;
pthread_mutex_init(&pool->mutex,NULL);
pthread_cond_init(&pool->cond,NULL);
// 初始化任务队列
taskqueue_init(&pool->queue);
// 初始化工作线程组
int i;
for(i=0;i<WORK_THREAD_COUNT;++i){
pthread_create(pool->tids+i,NULL,func,pool);
}
}
/**
* @brief 向线程池添加任务
*
* @param pool 线程池
* @param task 任务
* @return 添加成功与否
*/
bool threadpool_addtask(ThreadPool* pool,Task* task){
pthread_mutex_lock(&pool->mutex);
taskqueue_push(&pool->queue,task);
pthread_cond_signal(&pool->cond);
pthread_mutex_unlock(&pool->mutex);
}
/**
* @brief 销毁线程池
*
* @param pool 线程池
* @return 销毁结果
*/
bool threadpool_destroy(ThreadPool* pool){
pool->running = false;
pthread_cond_broadcast(&pool->cond);
// 销毁工作线程组
int i;
for(i=0;i<WORK_THREAD_COUNT;++i){
pthread_join(pool->tids[i],NULL);
}
// 销毁任务队列
taskqueue_destory(&pool->queue);
pthread_mutex_destroy(&pool->mutex);
pthread_cond_destroy(&pool->cond);
}
//main.c
#include <string.h>
#include "task_queue.h"
#include "thread_pool.h"
void test(){
printf("TID:%lld do something\n",pthread_self());
sleep(2);
}
int main(){
ThreadPool pool;
threadpool_init(&pool);
Task t = {test,NULL};
threadpool_addtask(&pool,&t);
threadpool_addtask(&pool,&t);
threadpool_addtask(&pool,&t);
threadpool_addtask(&pool,&t);
sleep(10);
threadpool_destroy(&pool);
}
#include <pthread.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define WORK_THREAD_COUNT 6
typedef struct Task{
void (*pfunc)();// 数据
struct Task* next; // 指针域
}Task;
typedef struct{
Task* head;
Task* tail;
}TaskQueue;
void taskqueue_init(TaskQueue* queue){
queue->head = queue->tail = NULL;
}
void taskqueue_destory(TaskQueue* queue){
Task* p = queue->head;
while(p != NULL){
Task* q = p->next;
free(p);
p = q;
}
queue->head = queue->tail = NULL;
}
void taskqueue_push(TaskQueue* queue,Task* task){
Task* copy = (Task*)malloc(sizeof(Task));
memcpy(copy,task,sizeof(Task));
if(NULL == queue->head){ // 入对第一个元素
printf("入队第一个元素:%p\n",copy);
queue->head = copy;
queue->tail = copy;
}else{ // 入队其他元素
printf("入队元素:%p\n",copy);
queue->tail->next = copy;
queue->tail = copy;
}
}
void taskqueue_pop(TaskQueue* queue){
if(queue->head == NULL) return;
Task* p = queue->head;
printf("出队元素:%p\n",p);
queue->head = p->next;
free(p);
}
Task* taskqueue_front(TaskQueue* queue){
return queue->head;
}
bool taskqueue_empty(TaskQueue* queue){
return queue->head == NULL;
}
/**
* @brief 线程池
*/
typedef struct{
pthread_t tids[WORK_THREAD_COUNT];///< 工作线程组
TaskQueue queue; ///< 任务队列
pthread_mutex_t mutex;// 互斥量
pthread_cond_t cond;// 条件变量
bool running;
}ThreadPool;
void* func(void* arg){
ThreadPool* pool = (ThreadPool*)arg;
pthread_mutex_lock(&pool->mutex);
// 如果没有任务
while(taskqueue_empty(&pool->queue)){
printf("TID:%ld wait\n",pthread_self());
pthread_cond_wait(&pool->cond,&pool->mutex);// 等待任务
if(!pool->running){
pthread_mutex_unlock(&pool->mutex);
pthread_exit(0);
}
}
// 取任务
printf("TID:%ld get task\n",pthread_self());
Task* task = taskqueue_front(&pool->queue);
void(*pfunc)() = task->pfunc;
taskqueue_pop(&pool->queue);
pthread_mutex_unlock(&pool->mutex);
pfunc();// 做任务
}
/**
* @brief 初始化线程池
*
* @param pool 线程池
* @return 初始化结果
*/
bool threadpool_init(ThreadPool* pool){
pool->running = true;
pthread_mutex_init(&pool->mutex,NULL);
pthread_cond_init(&pool->cond,NULL);
// 初始化任务队列
taskqueue_init(&pool->queue);
// 初始化工作线程组
int i;
for(i=0;i<WORK_THREAD_COUNT;++i){
pthread_create(pool->tids+i,NULL,func,pool);
}
}
/**
* @brief 向线程池添加任务
*
* @param pool 线程池
* @param task 任务
* @return 添加成功与否
*/
bool threadpool_addtask(ThreadPool* pool,Task* task){
pthread_mutex_lock(&pool->mutex);
taskqueue_push(&pool->queue,task);
pthread_cond_signal(&pool->cond);
pthread_mutex_unlock(&pool->mutex);
}
/**
* @brief 销毁线程池
*
* @param pool 线程池
* @return 销毁结果
*/
bool threadpool_destroy(ThreadPool* pool){
pool->running = false;
pthread_cond_broadcast(&pool->cond);
// 销毁工作线程组
int i;
for(i=0;i<WORK_THREAD_COUNT;++i){
pthread_join(pool->tids[i],NULL);
}
// 销毁任务队列
taskqueue_destory(&pool->queue);
pthread_mutex_destroy(&pool->mutex);
pthread_cond_destroy(&pool->cond);
}
void test(){
printf("TID:%lld do something\n",pthread_self());
sleep(2);
}
int main(){
ThreadPool pool;
threadpool_init(&pool);
Task t = {test,NULL};
threadpool_addtask(&pool,&t);
threadpool_addtask(&pool,&t);
threadpool_addtask(&pool,&t);
threadpool_addtask(&pool,&t);
sleep(10);
threadpool_destroy(&pool);
}