[smuxi: 1/37] ThreadPoolQueue: reduce lock{} time




commit f1a2f06c3f49a32d0d07a0fde837c96fffc9e865
Author: Andres G. Aragoneses <knocte gmail com>
Date:   Thu Oct 13 21:56:11 2016 +0800

    ThreadPoolQueue: reduce lock{} time
    
    This lock{} block didn't need to be so big, as it
    can end after the action is extracted from ActionQueue.

 src/Common/ThreadPoolQueue.cs | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
---
diff --git a/src/Common/ThreadPoolQueue.cs b/src/Common/ThreadPoolQueue.cs
index 46c7e7a5..d78bd580 100644
--- a/src/Common/ThreadPoolQueue.cs
+++ b/src/Common/ThreadPoolQueue.cs
@@ -55,6 +55,7 @@ namespace Smuxi.Common
 
         void CheckQueue()
         {
+            var action = default(Action);
             lock (ActionQueue) {
                 if (ActionQueue.Count == 0) {
                     return;
@@ -64,18 +65,18 @@ namespace Smuxi.Common
                     return;
                 }
 
-                var action = ActionQueue.Dequeue();
-                Interlocked.Increment(ref ActiveWorkers);
-
-                ThreadPool.QueueUserWorkItem(delegate {
-                    try {
-                        action();
-                    } finally {
-                        Interlocked.Decrement(ref ActiveWorkers);
-                        CheckQueue();
-                    }
-                });
+                action = ActionQueue.Dequeue();
             }
+            Interlocked.Increment(ref ActiveWorkers);
+
+            ThreadPool.QueueUserWorkItem(delegate {
+                try {
+                    action();
+                } finally {
+                    Interlocked.Decrement(ref ActiveWorkers);
+                    CheckQueue();
+                }
+            });
         }
     }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]