当前位置: 首页 » 产品 » 商务广告 » 正文

Linux线程池使用

放大字体  缩小字体 发布日期: 2025-03-19 21:10   来源:http://www.baidu.com/  作者:无忧资讯  浏览次数:14
核心提示:一、线程池概述线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务。线程池线程都是后

一、线程池概述

线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务。线程池线程都是后台线

程。每个线程都使用默认的堆栈大小,以默认的优先级运行,并处于多线程单元中。如果某个线程在托管代码中空闲(如正在等待

某个事件),则线程池将插入另一个辅助线程来使所有处理器保持繁忙。如果所有线程池线程都始终保持繁忙,但队列中包含挂起

的工作,则线程池将在一段时间后创建另一个辅助线程但线程的数目永远不会超过最大值。超过最大值的线程可以排队,但他们

要等到其他线程完成后才启动。应用程序可以有多个线程,这些线程在休眠状态中需要耗费大量时间来等待事件发生。其他线程

可能进入睡眠状态,并且仅定期被唤醒以轮循更改或更新状态信息,然后再次进入休眠状态。为了简化对这些线程的管理,为每

个进程提供了一个线程池,一个线程池有若干个等待操作状态,当一个等待操作完成时,线程池中的辅助线程会执行回调函数。

线程池中的线程由系统管理,程序员不需要费力于线程管理,可以集中精力处理应用程序任务。

使用线程池的好处:

1、减少在创建和销毁线程上所花的时间以及系统资源的开销;

2、如不使用线程池,有可能造成系统创建大量线程而导致消耗完系统内存以及”过度切换”。

在什么情况下使用线程池:

1、单个任务处理的时间比较短; 

2、将需处理的任务的数量大。

在什么情况下不使用线程池线程:

1、如果需要使一个任务具有特定优先级;

2、如果具有可能会长时间运行(并因此阻塞其他任务)的任务;

3、如果需要将线程放置到单线程单元中(线程池中的线程均处于多线程单元中);

4、如果需要永久标识来标识和控制线程,比如想使用专用线程来终止该线程,将其挂起或按名称发现它。

二、线程池程序

main.c

#include <stdio.h>#include <unistd.h>#include "thread_pool.h"void *task_test(void *arg){ printf("/t/tworking on task %d/n", (int)arg); sleep(1); return NULL;}int main (int argc, char *argv[]){ pool_t pool; int i=0; pool_init(&pool, 2);//初始化线程池 sleep(1); for(i=0; i < 5; i++){ sleep(1); pool_add_task(&pool, task_test, (void *)i);//向线程池中添加一个任务 } sleep(4); pool_uninit(&pool);//销毁线程池 return 0;}


thread_pool.c

#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <assert.h>#include "thread_pool.h"static void *pool_thread_server(void *arg);void pool_init(pool_t *pool, int threads_limit){ pool->threads_limit=threads_limit; pool->queue_head=NULL; pool->task_in_queue=0; pool->destroy_flag=0; pool->threadid=(pthread_t *)calloc(threads_limit, sizeof(pthread_t)); int i=0; pthread_mutex_init(&(pool->queue_lock), NULL); pthread_cond_init(&(pool->queue_ready), NULL); for (i=0; i < threads_limit; i++){ pthread_create(&(pool->threadid[i]), NULL, pool_thread_server, pool); } return;}int pool_uninit(pool_t *pool){ pool_task *head=NULL; int i; pthread_mutex_lock(&(pool->queue_lock)); if(pool->destroy_flag) return -1; pool->destroy_flag=1; pthread_mutex_unlock(&(pool->queue_lock)); pthread_cond_broadcast(&(pool->queue_ready)); for (i=0; i < pool->threads_limit; i++) pthread_join(pool->threadid[i], NULL); free(pool->threadid); pthread_mutex_lock(&(pool->queue_lock)); while(pool->queue_head !=NULL){ head=pool->queue_head; pool->queue_head=pool->queue_head->next; free(head); } pthread_mutex_unlock(&(pool->queue_lock)); pthread_mutex_destroy(&(pool->queue_lock)); pthread_cond_destroy(&(pool->queue_ready)); return 0;}static void enqueue_task(pool_t *pool, pool_task_f process, void *arg){ pool_task *task=NULL; pool_task *member=NULL; pthread_mutex_lock(&(pool->queue_lock)); if(pool->task_in_queue >=pool->threads_limit){ printf("task_in_queue > threads_limit!/n"); pthread_mutex_unlock (&(pool->queue_lock)); return; } task=(pool_task *)calloc(1, sizeof(pool_task)); assert(task !=NULL); task->process=process; task->arg=arg; task->next=NULL; pool->task_in_queue++; member=pool->queue_head; if(member !=NULL){ while(member->next !=NULL) member=member->next; member->next=task; }else{ pool->queue_head=task; } printf("/ttasks %d/n", pool->task_in_queue); pthread_cond_signal (&(pool->queue_ready)); pthread_mutex_unlock (&(pool->queue_lock));}static pool_task *dequeue_task(pool_t *pool){ pool_task *task=NULL; pthread_mutex_lock(&(pool->queue_lock)); if(pool->destroy_flag){ pthread_mutex_unlock(&(pool->queue_lock)); printf("thread 0x%lx will be destroyed/n", pthread_self()); pthread_exit(NULL); } if(pool->task_in_queue==0){ while((pool->task_in_queue==0) && (!pool->destroy_flag)){ printf("thread 0x%lx is leisure/n", pthread_self()); pthread_cond_wait(&(pool->queue_ready), &(pool->queue_lock)); } }else{ pool->task_in_queue--; task=pool->queue_head; pool->queue_head=task->next; printf("thread 0x%lx received a task/n", pthread_self()); } pthread_mutex_unlock(&(pool->queue_lock)); return task;}int pool_add_task(pool_t *pool, pool_task_f process, void *arg){ enqueue_task(pool, process, arg); return 0;}static void *pool_thread_server(void *arg){ pool_t *pool=NULL; pool=(pool_t *)arg; while(1){ pool_task *task=NULL; task=dequeue_task(pool); if(task !=NULL){ printf ("thread 0x%lx is busy/n", pthread_self()); task->process(task->arg); free(task); task=NULL; } } pthread_exit(NULL); return NULL;}
内容来源:https://www.16jixie.com/news/show-3198.html
 
 
[ 产品搜索 ]  [ 加入收藏 ]  [ 告诉好友 ]  [ 打印本文 ]  [ 违规举报 ]  [ 关闭窗口 ]

 

 
    行业协会  备案信息  可信网站