Turn from: http://blog.csdn.net/analogous_love/article/details/53319815
has been watching Zhang's "High-performance Linux Server Programming" book, download Link: http://download.csdn.net/detail/analogous_love/9673008
This is how the book describes the reactor model:
In this way, I write a simple exercise:
[CPP] View Plain copy /** * @desc: Practice server programs with reactor mode,main.cpp & nbsp;* @author: zhangyl * @date: 2016.11.23 */ #include <iostream> #include <string.h> #include < sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> //for htonl () and htons () # include <unistd.h> #include <fcntl.h> #include <sys/ epoll.h> #include <signal.h> //for signal () #include <pthread.h> #include <semaphore.h> #include <list> #include <errno.h> #include <time.h> # Include <sstream> #include <iomanip> //for std::setw ()/setfill () #include <stdlib.h> #define WORKER_THREAD_NUM 5 #define &NBSP;MIN (a, b) (a <= b) ? (a) : (b)) int g_epollfd = 0; bool g_bstop = false; int g_listenfd = 0; Pthread_t g_acceptthreadid = 0; pthread_t g_threadid[worker_thread_num] = { 0 }; pthread_cond_t g_acceptcond; pthread_mutex_t g_acceptmutex; pthread_cond_t g_cond /*= pthread_cond_initializer*/; Pthread_mutex_t g_mutex /*= PTHREAD_MUTEX_INITIALIZER*/; pthread_mutex_t g_clientmutex; &Nbsp; std::list<int> g_listclients; void prog_exit (Int signo) {