This article is Senlie original, reprint please retain this address:Http://blog.csdn.net/zhengsenlie
1. Pre-Create a thread pool and have each thread call the Accept
2. Use a mutex instead of having each thread block in the Accept call
The thread structure that is used to maintain information about each thread the typedef struct {pthread_tthread_tid;/* Threads ID */longthread_count;/* processing the number of connections */} Thread; thread*tptr;/* the thread structure pointer to a calloc-generated thread structure array *///global variables, including a listener socket and a mutex variable to be shared by all threads INTLISTENFD, Nthreads;socklen _taddrlen;pthread_mutex_tmlock;/* include serv07 */#include "unpthread.h" #include "pthread07.h" Pthread_mutex_tmlock = Pthread_mutex_initializer;intmain (int argc, char **argv) {inti;voidsig_int (int), thread_make (int),//1. Create a listen socket if ( ARGC = = 3) LISTENFD = Tcp_listen (NULL, argv[1], &addrlen); else if (argc = = 4) LISTENFD = Tcp_listen (argv[1], argv[2], &A Mp;addrlen) Elseerr_quit ("usage:serv07 [
UNIX Network Programming Volume 1 server Programming Paradigm 7 pre-create thread to protect accept with mutex lock mode