记录一下学习线程池的过程,代码用到的函数归结:
pthread_mutex_lock
pthread_mutex_unlock
pthread_cond_wait
pthread_cond_signal
pthread_cond_broadcast
pthread_create
pthread_join
程序中还用到了链表,
还有一个知识点:任何类型的数据都可以是void类型,
但void类型在使用之前必须进行强制类型转换。
/*
*Author:Greens_Ren
*Description:线程池
*/
/*头文件*/
#include<pthread.h>
#include<stdio.h>
/* begin : add */
#include<stdlib.h>
#include<unistd.h>
#include<assert.h>
#include<sys/types.h>
/* end : add */
/*数据结构*/
typedef struct Thread_worker
{
void *(*worker)(void *arg);
void * arg;
struct Thread_worker *next;
}CThread_worker;
typedef struct Thread_pool
{
pthread_mutex_t queue_lock;
pthread_cond_t queue_ready;
int max_thread_num;
pthread_t *phead_threadid;
int cur_queue_size;
CThread_worker *phead;
int shutdown;
}CThread_pool;
/*全局区*/
static CThread_pool *pool = NULL;
/*函数*/
void pthread_init(int max_thread_num);
void *thread_roution(void * arg);
void pthread_add_worker(void *(*worker)(void *arg), void * arg);
void pthread_destroy(void);
void