Unix:线程池的例子

/*
 * 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 resource
CThread_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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值