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
- Unix:线程池的例子
- java线程池的例子
- 线程池的一个例子
- Unix Domain Socket的例子
- 转载 一个C#线程池的例子
- 对于python实现线程池的例子
- 使用boost线程池很好的例子
- 扩展线程池ThreadPoolExecutor的简单例子
- 一个线程池的例子(一)
- 一个线程池的例子(二)
- 一个线程池的例子(三)
- 使用java线程池的一个例子
- 线程的例子
- 线程的例子
- 线程同步的例子
- 线程 买票的例子
- 线程的简单例子
- 由Java线程池的例子到Tomcat线程池
- Oracle 删除数据的几种方法
- HDU 2009 求数列的和
- AIX 夏令时
- iOS开发中常用但经常忘记的技巧
- 我害怕阅读的人
- Unix:线程池的例子
- Nginx代码解析
- 贪心算法—Problem A
- 点击动画 波纹效果 主要用来做引导动画
- HttpClient4 基本操作
- 关于C++学习
- 一篇基于pthon和scikt-learn的关于机器学习的介绍
- hrbust 哈理工oj 1989 营救小组【BFS】
- 使用jdbclogger将所有sql完整的打印出来