Kaynağa Gözat

adapt #141: setThreadNum(0)

ithewei 3 yıl önce
ebeveyn
işleme
80099a5912
4 değiştirilmiş dosya ile 21 ekleme ve 12 silme
  1. 4 2
      README-CN.md
  2. 4 2
      README.md
  3. 1 5
      evpp/EventLoopThreadPool.h
  4. 12 3
      evpp/TcpServer.h

+ 4 - 2
README-CN.md

@@ -168,7 +168,8 @@ int main() {
     srv.setThreadNum(4);
     srv.setThreadNum(4);
     srv.start();
     srv.start();
 
 
-    while (1) hv_sleep(1);
+    // press Enter to stop
+    while (getchar() != '\n');
     return 0;
     return 0;
 }
 }
 ```
 ```
@@ -245,7 +246,8 @@ int main() {
     };
     };
     cli.start();
     cli.start();
 
 
-    while (1) hv_sleep(1);
+    // press Enter to stop
+    while (getchar() != '\n');
     return 0;
     return 0;
 }
 }
 ```
 ```

+ 4 - 2
README.md

@@ -165,7 +165,8 @@ int main() {
     srv.setThreadNum(4);
     srv.setThreadNum(4);
     srv.start();
     srv.start();
 
 
-    while (1) hv_sleep(1);
+    // press Enter to stop
+    while (getchar() != '\n');
     return 0;
     return 0;
 }
 }
 ```
 ```
@@ -234,7 +235,8 @@ int main() {
     };
     };
     cli.start();
     cli.start();
 
 
-    while (1) hv_sleep(1);
+    // press Enter to stop
+    while (getchar() != '\n');
     return 0;
     return 0;
 }
 }
 ```
 ```

+ 1 - 5
evpp/EventLoopThreadPool.h

@@ -50,14 +50,10 @@ public:
     void start(bool wait_threads_started = false,
     void start(bool wait_threads_started = false,
                std::function<void(const EventLoopPtr&)> pre = NULL,
                std::function<void(const EventLoopPtr&)> pre = NULL,
                std::function<void(const EventLoopPtr&)> post = NULL) {
                std::function<void(const EventLoopPtr&)> post = NULL) {
+        if (thread_num_ == 0) return;
         if (status() >= kStarting && status() < kStopped) return;
         if (status() >= kStarting && status() < kStopped) return;
         setStatus(kStarting);
         setStatus(kStarting);
 
 
-        if (thread_num_ == 0) {
-            setStatus(kRunning);
-            return;
-        }
-
         std::shared_ptr<std::atomic<int>> started_cnt(new std::atomic<int>(0));
         std::shared_ptr<std::atomic<int>> started_cnt(new std::atomic<int>(0));
         std::shared_ptr<std::atomic<int>> exited_cnt(new std::atomic<int>(0));
         std::shared_ptr<std::atomic<int>> exited_cnt(new std::atomic<int>(0));
 
 

+ 12 - 3
evpp/TcpServer.h

@@ -45,6 +45,8 @@ public:
     void setMaxConnectionNum(uint32_t num) {
     void setMaxConnectionNum(uint32_t num) {
         max_connections = num;
         max_connections = num;
     }
     }
+
+    // NOTE: totalThreadNum = 1 acceptor_thread + N worker_threads (N can be 0)
     void setThreadNum(int num) {
     void setThreadNum(int num) {
         worker_threads.setThreadNum(num);
         worker_threads.setThreadNum(num);
     }
     }
@@ -60,13 +62,17 @@ public:
     }
     }
 
 
     void start(bool wait_threads_started = true) {
     void start(bool wait_threads_started = true) {
-        worker_threads.start(wait_threads_started);
+        if (worker_threads.threadNum() > 0) {
+            worker_threads.start(wait_threads_started);
+        }
         acceptor_thread.start(wait_threads_started, std::bind(&TcpServerTmpl::startAccept, this));
         acceptor_thread.start(wait_threads_started, std::bind(&TcpServerTmpl::startAccept, this));
     }
     }
     // stop thread-safe
     // stop thread-safe
     void stop(bool wait_threads_stopped = true) {
     void stop(bool wait_threads_stopped = true) {
         acceptor_thread.stop(wait_threads_stopped);
         acceptor_thread.stop(wait_threads_stopped);
-        worker_threads.stop(wait_threads_stopped);
+        if (worker_threads.threadNum() > 0) {
+            worker_threads.stop(wait_threads_stopped);
+        }
     }
     }
 
 
     int withTLS(const char* cert_file, const char* key_file) {
     int withTLS(const char* cert_file, const char* key_file) {
@@ -185,7 +191,10 @@ private:
         hio_detach(connio);
         hio_detach(connio);
         // Load Banlance: Round-Robin
         // Load Banlance: Round-Robin
         EventLoopPtr worker_loop = server->worker_threads.nextLoop();
         EventLoopPtr worker_loop = server->worker_threads.nextLoop();
-        worker_loop->queueInLoop(std::bind(&TcpServerTmpl::newConnEvent, connio));
+        if (worker_loop == NULL) {
+            worker_loop = server->acceptor_thread.loop();
+        }
+        worker_loop->runInLoop(std::bind(&TcpServerTmpl::newConnEvent, connio));
     }
     }
 
 
 public:
 public: