epoll實現Reactor模式

來源:互聯網
上載者:User

轉自:http://blog.csdn.net/analogous_love/article/details/53319815

最近一直在看遊雙的《高效能Linux伺服器編程》一書,下載連結: http://download.csdn.net/detail/analogous_love/9673008

書上是這麼介紹Reactor模式的:




按照這個思路,我寫個簡單的練習:

[cpp]  view plain  copy   /**    *@desc:   用reactor模式練習伺服器程式,main.cpp   *@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 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;      std::list<int> g_listClients;      void prog_exit(int signo)   {  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.