Selaa lähdekoodia

fix #391: idx out of range

ithewei 2 vuotta sitten
vanhempi
commit
321a5407dc

+ 1 - 1
evpp/EventLoopThreadPool.h

@@ -34,7 +34,7 @@ public:
         int idx = 0;
         if (lb == LB_RoundRobin) {
             if (++next_loop_idx_ >= numLoops) next_loop_idx_ = 0;
-            idx = next_loop_idx_.fetch_and(numLoops);
+            idx = next_loop_idx_ % numLoops;
         } else if (lb == LB_Random) {
             idx = hv_rand(0, numLoops - 1);
         } else if (lb == LB_LeastConnections) {

+ 2 - 2
examples/multi-thread/one-acceptor-multi-workers.c

@@ -19,10 +19,10 @@ static hloop_t** worker_loops = NULL;
 
 static hloop_t* get_next_loop() {
     static int s_cur_index = 0;
-    if (s_cur_index == thread_num) {
+    if (++s_cur_index >= thread_num) {
         s_cur_index = 0;
     }
-    return worker_loops[s_cur_index++];
+    return worker_loops[s_cur_index % thread_num];
 }
 
 static void on_close(hio_t* io) {