Quellcode durchsuchen

优化ThreadPool纤程调度,每次loop update fiber数量/线程池线程数量,这样避免线程一直无法sleep导致cpu一直100%

tanghai vor 2 Jahren
Ursprung
Commit
bcec40d498

+ 5 - 0
Unity/Assets/Scripts/Core/World/Module/Fiber/FiberManager.cs

@@ -126,5 +126,10 @@ namespace ET
             this.fibers.TryGetValue(id, out Fiber fiber);
             return fiber;
         }
+
+        public int Count()
+        {
+            return this.fibers.Count;
+        }
     }
 }

+ 10 - 0
Unity/Assets/Scripts/Core/World/Module/Fiber/ThreadPoolScheduler.cs

@@ -28,8 +28,18 @@ namespace ET
 
         private void Loop()
         {
+            int count = 0;
             while (true)
             {
+                if (count == 0)
+                {
+                    count = this.fiberManager.Count() / this.threads.Count;
+                    Thread.Sleep(1);
+                    continue;
+                }
+
+                --count;
+                
                 if (this.fiberManager.IsDisposed())
                 {
                     return;