/*
* 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;
}
Unix:線程池的例子
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.