This article is Senlie original, reprint please retain this address: Http://blog.csdn.net/zhengsenlie
1. After a thread pool is created in the program startup phase, only the main thread calls accept and passes the client connection to one of the available threads in the pool.
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 an array of thread structures generated with calloc *///define a shared array that holds the connected socket descriptor #definemaxncli32intclifd[maxncli], Iget, iput;pthread_mutex_tclifd_mutex;pthread_cond_tclifd_cond;/* include serv08 */#include "unpthread.h" #include " Pthread08.h "static Intnthreads;pthread_mutex_tclifd_mutex = Pthread_mutex_initializer;pthread_cond_tclifd_cond = Pthread_cond_initializer;intmain (int argc, char **argv) {inti, LISTENFD, connfd;voidsig_int (int), thread_make (int); Socklen_taddrlen, Clilen;struct sockaddr*cliaddr;//1. Create a listener socket if (argc = = 3) LISTENFD = Tcp_listen (NULL, argv[1], & Addrlen); else if (argc = = 4) LISTENFD = Tcp_listen (argv[1], argv[2], &addrlen); Elseerr_quit ("usage:serv08"
UNIX Network Programming Volume 1 server Programming Paradigm 8 pre-create thread, call accept from main thread