多线程存在的问题:进程所支持的线程数量问题(受限)
int thread_num //已开启的线程的数量
struct job *head; //任务队列的头
struct job *tail; //任务队列的尾
int queue_max_num //任务队列的最大数
int queue_cur_num //任务队列已有多少个任务
pthread_t *pthread_ids //保存线程池中线程的id
pthread_cond_t queue_empty;//任务队列为空的条件
pthread_cond_t queue_not_empty;//任务队列不为空的条件
pthread_cond_t queue_not_full;//任务队列不为满的条件
每个线程输出hello world和welcome to china
1、任务队列
(1)任务队列为空
(2)任务队列为满
(3)任务队列为不为空
任务队列为空时,线程池里的线程阻塞等待
任务队列为满,不能再添加新的任务
任务队列不为空时,线程池里的线程处理任务
2、线程池
(1)线程池里的线程数量
(2)线程池里的工作线程数量
(3)任务队列的大小
(4)任务队列锁
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<pthread.h>
#include<unistd.h>
struct job
{
void *(*func)(void *arg);
void *arg;
struct job *next;
};
struct threadpool
{
int thread_num;
pthread_t *pthread_ids;
struct job *head;
struct job *tail;
int queue_max_num;
int queue_cur_num;
pthread_mutex_t mutex;
pthread_cond_t queue_empty;
pthread_cond_t queue_not_empty;
pthread_cond_t queue_not_full;
};
void *threadpool_function(void *arg)
{
struct threadpool *pool = (struct threadpool *)arg;
struct job *pjob = NULL;
while(1)
{
pthread_mutex_lock(&(pool->mutex));
while(pool->queue_cur_num == 0)
{
pthread_cond_wait(&(pool->queue_not_empty),&(pool->mutex));
}
pjob = pool->head;
pool->queue_cur_num--;
if(pool->queue_cur_num == 0)
{
pool->head = pool->tail = NULL;
}
else
{
pool->head = pool->head->next;
}
//释放结点
pthread_mutex_unlock(&(pool->mutex));
(*(pjob->func))(pjob->arg);
free(pjob);
pjob = NULL;
}
}
struct threadpool *threadpool_init(int thread_num, int queue_max_num)
{
struct threadpool *pool = (struct threadpool *)malloc(sizeof(struct threadpool));//kai pi kong jian
//malloc
pool->queue_max_num = thread_num;
pool->queue_cur_num = 0;
pool->head = NULL;
pool->tail = NULL;
pthread_mutex_init(&(pool->mutex), NULL);
pthread_cond_init(&(pool->queue_empty), NULL);//初始化条件
pthread_cond_init(&(pool->queue_not_empty), NULL);
pthread_cond_init(&(pool->queue_not_full), NULL);
pool->thread_num = thread_num;
pool->pthread_ids = (pthread_t *)malloc(sizeof(pthread_t) * thread_num);
for (int i = 0; i <pool->thread_num; i++)
{
pthread_create(&(pool->pthread_ids[i]), NULL, threadpool_function, (void *)pool);
}
return pool;
};
void threadpool_add_job(struct threadpool *pool, void *(func)(void *), void *arg)
{
//锁
pthread_mutex_lock(&(pool->mutex));
while(pool->queue_cur_num == pool->queue_max_num)
{
pthread_cond_wait((&pool->queue_not_full), &(pool->mutex));
}
struct job *pjob = (struct job*)malloc(sizeof(struct job));
//malloc
pjob->func = func;
pjob->arg = arg;
pjob->next = NULL;
//pjob->func(pjob->arg);
if(NULL == pool->head)
{
pool->head = pool->tail = pjob;
}
else
{
pool->tail->next = pjob;
pool->tail = pjob;
}
pool->queue_cur_num++;
printf("%d\n",pool->queue_cur_num);
pthread_mutex_unlock(&(pool->mutex));
}
void * work (void *arg)
{
char *p = (char*)arg;
printf("hello world! %s\n",p);
printf("welcome to China! %s\n",p);
sleep(1);
}
int main()
{
struct threadpool *pool = threadpool_init(10,100);
threadpool_add_job(pool,work, "1");
threadpool_add_job(pool,work, "2");
threadpool_add_job(pool,work, "3");
threadpool_add_job(pool,work, "4");
threadpool_add_job(pool,work, "5");
threadpool_add_job(pool,work, "6");
threadpool_add_job(pool,work, "7");
threadpool_add_job(pool,work, "8");
threadpool_add_job(pool,work, "9");
threadpool_add_job(pool,work, "10");
return 0;
}
root@jsetc-virtual-machine:~/0412# gcc thread_pool.c -lpthread
root@jsetc-virtual-machine:~/0412# ./a.out
1
2
3
4
hello world! 1
welcome to China! 1
4
5
6
7
8
9