Linux C线程池

在Linux下写一个线程池以及线程池的一些用法和注意点(已网页展示为例)

-->线程池介绍(大部分来自网络)

   在这个部分,详细的介绍一下线程池的作用以及它的技术背景以及他提供的一些服务等。大部分内容来自我日常生活中在网络中学习到的一些概念性的东西。

-->代码

   测试一下,具体的实现。

-->代码下载

--------------------------------------------------------------------------------------------------------------------------------------

-->线程池介绍

1、技术背景:服务器程序利用线程技术响应客户请求已经司空见惯,但是线程的使用是有待优化和处理的。单线程执行并不是一个高效的方式,这个时候可能就要考虑高并发,多线程等方式。线程池也是线程优化的一种方式。

在面向对象的过程中,对象的创建和销毁是非常占资源的,每创建一个对象都要获取内存资源以及其他一些资源。在Java中更是如此,他要跟踪每一个对象,在它使用完毕的时候,自动的销毁它并垃圾回收。可想而知,运行的速度之慢。这就产生了“池化技术”。

2、线程池如何提高服务器程序的性能?

     ●T1 = 创建线程

   ●T2 = 执行线程 包括访问共享数据、线程同步等

   ●T3 = 销毁线程

   ●T = T1 + T2 + T3   

  单线程的情况下,系统花大量的时间再T1、T3阶段。我们要采取最优的措施,减少T1和T3的系统开销。线程池不仅调整T1,T3产生的时间段,而且它还显著减少了创建线程的数目。

假设:一台服务器,每天要处理10万个请求,这时候,我们比较不用线程池的技术和用线程池,他们的区别。
如果没有用线程池的话,那么程序将会用大把的时候来创建这10万个线程,用线程池,我们一般可用的线程数不会大于10万,所以可以大大减小开销。

3、具体工作流程

    线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务。线程池线程都是后台线程。每个线程都使用默认的堆栈大小,以默认的优先级运行,并处于多线程单元中。如果某个线程在托管代码中空闲(如正在等待某个事件),则线程池将插入另一个辅助线程来使所有处理器保持繁忙。如果所有线程池线程都始终保持繁忙,但队列中包含挂起的工作,则线程池将在一段时间后创建另一个辅助线程但线程的数目永远不会超过最大值。超过最大值的线程可以排队,但他们要等到其他线程完成后才启动。

4、何时不使用线程池线程  

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

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

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

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

5、一般使用线程池的程序的特点

    ● 需要花费大量的时候,并且请求的时间比较短。

    ● 对性能要求苛刻的应用,比如要求服务器迅速响应客户请求。

    ● 接受突发性的大量请求,但不至于使服务器因此产生大量线程的应用。

 

-->代码

//threadpool.h
#ifndef __THREAD_POOL__
#define __THREAD_POOL__

typedef struct pool_task
{
    void *(*process)(void *arg);
    void *arg;
    struct pool_task *next;
}pool_task;

typedef struct pool
{
    pool_task *queue_head;

    pthread_mutex_t queue_lock;
    pthread_cond_t queue_ready;

    pthread_t *threadid;

    int threads_limit;    
    int task_in_queue;
    int destroy_flag;
}pool_t;

typedef void *(*pool_task_f)(void *arg);

#endif

 

 

//功能:线程池
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
 
#include "threadpool.h"
 
static void *pool_thread_server(void *arg);
 
/*********************************************************************
*功能:        初始化线程池结构体并创建线程
*参数:        
            pool:线程池句柄
            threads_limit:线程池中线程的数量
*返回值:    无
*********************************************************************/
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;
    /*创建存放线程ID的空间*/
    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);
    /*循环创建threads_limit个线程*/
    for (i = 0; i < threads_limit; i++){
        pthread_create(&(pool->threadid[i]), NULL, pool_thread_server, pool);
    }
    return;
}
 
/*********************************************************************
*功能:        销毁线程池,等待队列中的任务不会再被执行,
            但是正在运行的线程会一直,把任务运行完后再退出
*参数:        线程池句柄
*返回值:    成功:0,失败非0
*********************************************************************/
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;
}
 
/*********************************************************************
*功能:        向任务队列中添加一个任务
*参数:        
            pool:线程池句柄
            process:任务处理函数
            arg:任务参数
*返回值:    无
*********************************************************************/
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);
    }
    /* 如果等待队列为0并且不销毁线程池,则处于阻塞状态 */
    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是一个原子操作,等待前会解锁,唤醒后会加锁 */
            pthread_cond_wait(&(pool->queue_ready), &(pool->queue_lock));
        }
    }else{
        /* 等待队列长度减去1,并取出队列中的第一个元素 */
        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;
}
 
/*********************************************************************
*功能:        向线程池中添加一个任务
*参数:        
            pool:线程池句柄
            process:任务处理函数
            arg:任务参数
*返回值:    0
*********************************************************************/
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;
}



 

 

//服务端监听客服端访问网页的请求
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <fcntl.h>
#include "threadpool.h"
 
void wb_fun(int sockfd,char *buff,int buff_len)
{    
    char head[]="HTTP/1.1 200 OK\r\n"                    \
                 "Content-Type: text/html\r\n"        \
                 "\r\n";
    char err[]=    "HTTP/1.1 404 Not Found\r\n"        \
                 "Content-Type: text/html\r\n"        \
                 "\r\n"                                \
                 "<HTML><BODY>File not found</BODY></HTML>";
 
    char         filename[1024] = "";  //文件名
    char         file[1024] = ""; //存储文件内容
    uint32_t    len = 0;
    
    if(sscanf(buff, "GET /%s", filename) != 0)
    {
        int fd = open(filename,O_RDONLY);//打开文件
        if(fd == -1)
        {       //打开失败
            send(sockfd, err, strlen(err), 0);
            perror("open");
        }
        else
        {
            send(sockfd, head, strlen(head), 0);
            bzero(file, sizeof(file));
            printf("open file:%s\n",filename);
            while ( (len = read(fd, file, sizeof(file))) != 0)
            {//读取文件
                send(sockfd, file, len, 0);//发送文件
                bzero(file, sizeof(file));
            }
            close(fd);
            printf("close file:%s\n",filename);
        }
    }    
}
 
void *task_test(void *arg)
{
    char recv_buff[1024];
    int  fd = (int) arg;

    bzero(recv_buff, sizeof(recv_buff));
    int len = recv(fd, recv_buff, sizeof(recv_buff), 0);//接收数据
    if(len >0)
    {
        printf("recv data :%s\n",recv_buff);
        wb_fun(fd,recv_buff,sizeof(recv_buff));                                  
    }
    close(fd); //关闭连接    
    return NULL;
}
 
int main(int argc, char *argv[])
{
    struct sockaddr_in addr;
    bzero(&addr,sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(13000);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
 
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
 
    int temp = 1;
    setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR, &temp, sizeof(int));
    int res = bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
    if(res != 0)
    {
        perror("bind");
    }
    //修改套接字为被动属性
    listen(sockfd, 5);    
    printf("sockfd:%d\n", sockfd);                                            
 
    //创建文件监听                                                                
    fd_set readfds,tmpfds;

    FD_ZERO(&readfds);
    FD_ZERO(&tmpfds);
    FD_SET(sockfd, &readfds);//将sockfd 加入读属性变化文件描述符
    
    //线程池 描述结构体
    pool_t pool;                    
    pool_init(&pool, 5);//初始化一个线程池,其中创建2个线程
    
    
    int fd = 0;
    while(1)
    {
        tmpfds = readfds;
        res = select(1024, &tmpfds, NULL, NULL, NULL);
        if(res > 0)
        {
            for(fd = 0; fd < 1024; fd ++)
            {
                if(FD_ISSET(fd, &tmpfds))
                {//检查1~1024 谁在内
                    printf("fd:%d\n", fd);
                    if(fd == sockfd)
                    {//新连接建立
                        int connect = accept(fd, NULL, NULL);
                        printf("connect fd:%d\n", connect);
                        FD_SET(connect, &readfds);//将新连接加入 监听
                    }
                    else
                    {
                        pool_add_task(&pool, task_test, (void *)fd);//添加一个任务
                        FD_CLR(fd, &readfds); //清除描述符
                    }
                }
            }
        }
    }
    pool_uninit(&pool);//删除线程池
    close(sockfd);
    return 0;
}

 

1.运行结果:

2、客服端http请求:(根据自己的服务端进行IP修改,端口号可以在主函数里更改)

http://192.168.2.159:13000/html/index.html

**********需下载完整工程文件,点击下方************

https://download.csdn.net/download/qq_23118623/11250663

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值