自己做了个线程池,问题多多,主要是多线程编程不怎么会。
#ifndef _PTHREAD_POOL_H
#define _PTHREAD_POOL_H
#include <pthread.h>
typedef void (*func_t)(void *args);
struct task_t
{
struct task_t *next;
func_t func;
void *args;
};
struct tasklist_t
{
struct task_t *head;
struct task_t *tail;
pthread_mutex_t lock;
pthread_cond_t not_empty;
pthread_cond_t not_full;
int taskcnt;
int task_max;
void (*task_add)(struct tasklist_t *, func_t func, void *args);
func_t (*task_get)(struct tasklist_t *, void **args);
};
struct pthreadpool_t
{
pthread_t *pids;
int maxpnum;
struct tasklist_t tasklist;
void (*tpool_init)(struct pthreadpool_t *tpool, int pnum, int tm);
void *(*do_pthread)(void *);
void (*tpool_wait)(struct pthreadpool_t *);
};
#endif
|
#include "pthreadpool.h"
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define DEBUG 1
func_t task_get(struct tasklist_t *list, void **args)
{
pthread_mutex_lock(&list->lock);
while(list->taskcnt == 0)
pthread_cond_wait(&list->not_empty, &list->lock);
struct task_t *tmp;
func_t func;
tmp = list->head->next;
func = tmp->func;
*args =tmp->args;
free(list->head);
list->head = tmp;
list->taskcnt--;
#if 0
printf("taskcnt = %d\n", list->taskcnt);
#endif
pthread_cond_broadcast(&list->not_full);
pthread_mutex_unlock(&list->lock);
return func;
}
void task_add(struct tasklist_t *list, func_t func, void *args)
{
pthread_mutex_lock(&list->lock);
while(list->taskcnt == list->task_max)
pthread_cond_wait(&list->not_full, &list->lock);
struct task_t *new = malloc(sizeof(*new));
new->func = func;
new->args = args;
new->next = NULL;
list->tail->next = new;
list->tail = new;
list->taskcnt++;
#if 0
printf("task_add sucess\n");
func(NULL);
#endif
pthread_cond_broadcast(&list->not_empty);
pthread_mutex_unlock(&list->lock);
}
void *do_pthread(void *tp)
{
func_t func;
struct pthreadpool_t *tpool = tp;
void *args;
while(1)
{
#if 0
printf("do_pthread\n");
#endif
func = tpool->tasklist.task_get(&tpool->tasklist, &args);
(void)func(args);
}
}
void tpool_wait(struct pthreadpool_t *tpool)
{
int i;
for(i = 0; i < tpool->maxpnum; i++)
pthread_join(tpool->pids[i], NULL);
}
void tpool_init(struct pthreadpool_t *tpool, int pnum, int tm)
{
tpool->pids = malloc(pnum * sizeof(pthread_t));
tpool->maxpnum = pnum;
tpool->tasklist.task_max = tm;
pthread_mutex_init(&tpool->tasklist.lock, NULL);
pthread_cond_init(&tpool->tasklist.not_empty, NULL);
pthread_cond_init(&tpool->tasklist.not_full, NULL);
tpool->tasklist.head = tpool->tasklist.tail = malloc(sizeof(*tpool->tasklist.head));
tpool->tasklist.taskcnt = 0;
tpool->tasklist.task_add = task_add;
tpool->tasklist.task_get = task_get;
tpool->do_pthread = do_pthread;
tpool->tpool_init = tpool_init;
int i;
for(i = 0; i < pnum; i++){
int ret = pthread_create(&tpool->pids[i],
NULL,
tpool->do_pthread,
tpool);
}
}
|
测试函数:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "pthreadpool.h"
int i=0;
void print(void *a)
{
int j;
for(j = 0; j < INT_MAX /1000; j++);
printf("%s i am %ld\n",(char *)a, pthread_self());
}
int main(void)
{
struct pthreadpool_t *tpool = malloc(sizeof(*tpool));
tpool_init(tpool, 10, 100);
while(1){
char *s = malloc(10);
strcpy(s, "hello");
tpool->tasklist.task_add(&tpool->tasklist, print ,s);
free(s);
}//tpool->tpool_wait(tpool);
//free(tpool);
}
|
|
文件: | test_threadpool.tar.gz |
大小: | 1KB |
下载: | 下载 |
|
阅读(3891) | 评论(0) | 转发(0) |