Unix:线程池的例子

来源:互联网 发布:五五开这个人 知乎 编辑:程序博客网 时间:2024/06/05 09:59
/* * pthread_pool.c * *  Created on: 2016-3-8 *      Author: xfhu */#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <pthread.h>#include <assert.h>#include "pthread_pool.h"//share resourceCThread_pool pool ;//线程池对象,全局共享extern "C" void* thread_routine (CThread_pool* pool);CThread_pool::CThread_pool(){    pthread_mutex_init (&(queue_lock), NULL);    pthread_cond_init (&(queue_ready), NULL);    queue_head = NULL;    max_thread_num = default_max_thread;    cur_queue_size = 0;    shutdown = 0;    threadid = (pthread_t *) malloc (max_thread_num * sizeof (pthread_t));    int i = 0;    //创建max_thread_num个线程    for (i = 0; i < max_thread_num; i++) {        pthread_create (&(threadid[i]), NULL, (void* (*)(void*))thread_routine,(void*)this);    }}CThread_pool::CThread_pool(int max_thread){    pthread_mutex_init (&(queue_lock), NULL);    pthread_cond_init (&(queue_ready), NULL);    queue_head = NULL;    max_thread_num = max_thread;    cur_queue_size = 0;    shutdown = 0;    threadid = (pthread_t *) malloc (max_thread_num * sizeof (pthread_t));    int i = 0;    //创建max_thread_num个线程    for (i = 0; i < max_thread_num; i++) {        pthread_create (&(threadid[i]), NULL, (void* (*)(void*))thread_routine,(void*)this);    }}//销毁线程池CThread_pool::~CThread_pool(){    int i;    shutdown = 1;    pthread_cond_broadcast (&(queue_ready));    //回收所有线程占用资源    for (i = 0; i < max_thread_num; i++){        pthread_join (threadid[i], NULL);    }    //释放线程ID占用内存    free (threadid);    //清理线程池中没有完成的任务    Thread_worker *head = NULL;    while (queue_head != NULL){        head = queue_head;        queue_head = queue_head->next;        free (head);    }    //释放互斥量和条件变量    pthread_mutex_destroy(&(queue_lock));    pthread_cond_destroy(&(queue_ready));    //释放线程池对象占用内存。    return ;}//向线程池添加任务int CThread_pool::pool_add_worker (void *(*process)(void *arg), void *arg) {    //创建一个新任务    Thread_worker *newworker = (Thread_worker *) malloc (sizeof (Thread_worker));    newworker->process = process;    newworker->arg = arg;    newworker->next = NULL;    //将任务添加到线程池任务队列链表    pthread_mutex_lock (&(queue_lock));    Thread_worker *member = queue_head;    if (member != NULL) {        while (member->next != NULL){            member = member->next;        }        member->next = newworker;    } else {        queue_head = newworker;    }    cur_queue_size++;    pthread_mutex_unlock (&(queue_lock));    //使条件变量变成真,唤醒阻塞在条件变量上的线程    pthread_cond_signal (&(queue_ready));    return 0;}Thread_worker * CThread_pool::thread_get_task() {    Thread_worker *worker = NULL;    pthread_mutex_lock (&(queue_lock));    while (cur_queue_size == 0 && !shutdown) {        //printf ("thread 0x%x is waiting\n", pthread_self ());        pthread_cond_wait (&(queue_ready), &(queue_lock));    }    if (shutdown) {        pthread_mutex_unlock (&(queue_lock));        printf ("thread 0x%x will exit\n", (unsigned int)pthread_self ());        return NULL;    }    //printf ("thread 0x%x is starting to work\n", pthread_self ());    cur_queue_size--;    worker = queue_head;    queue_head = worker->next;    pthread_mutex_unlock (&(queue_lock));    return worker;}void* thread_routine (CThread_pool* pool) {    printf ("starting thread 0x%x\n", (unsigned int)pthread_self ());    while (1) {        Thread_worker *worker = pool->thread_get_task();        if(worker!=NULL) {            (*(worker->process)) (worker->arg);        } else{            break;        }    }    pthread_exit (NULL);}void *myprocess (void *arg) {    printf ("threadid is 0x%x, working on task %d\n", (unsigned int)pthread_self (),*(int *) arg);    sleep (1);    return NULL;}int  main (int argc, char **argv) {    CThread_pool* pool = new CThread_pool();    int *workingnum = (int *) malloc (sizeof (int) * 10);    int i;    for (i = 0; i < 10; i++) {        workingnum[i] = i;        pool->pool_add_worker(myprocess, &workingnum[i]);    }    sleep (5);    delete pool;    free (workingnum);    return 0;}
0 0
原创粉丝点击