日期:2014-05-16  浏览次数:20692 次

linux 2.6内核epoll用法举例说明(续)--给echo服务器增加读线程池
上篇文章使用linux内核2.6提供的epoll机制实现了一个反应式echo服务器,使用反应式服务器的最大好处就是可以按cpu的数量来配置线程池 内线程的线程数而不是根据客户端的并发量配置线程池。我是第一次使用pthread库来写线程池,使用的是工作队列方式的线程池。我感觉作队列方式的线程 池可以当成一种设计模式来用的,在很多平台上都是可以按这种方式来实现线程池,从win32 ,unix到jvm都是适用的

?


#include <iostream>

#include <sys/socket.h>

#include <sys/epoll.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <fcntl.h>

#include <unistd.h>

#include <stdio.h>

#include <pthread.h>

?

#define MAXLINE 10

#define OPEN_MAX 100

#define LISTENQ 20

#define SERV_PORT 5555

#define INFTIM 1000

?

//线程池任务队列结构体

struct task{

??int fd; ???????????//需要读写的文件描述符

??struct task *next; //下一个任务

};

?

//用于读写两个的两个方面传递参数

struct user_data{

??int fd;

??unsigned