summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--lib/system/threads.nim15
1 files changed, 10 insertions, 5 deletions
diff --git a/lib/system/threads.nim b/lib/system/threads.nim
index 86af95a85..5cdca5470 100644
--- a/lib/system/threads.nim
+++ b/lib/system/threads.nim
@@ -465,17 +465,22 @@ proc handle*[TArg](t: Thread[TArg]): SysThread {.inline.} =
   result = t.sys
 
 when hostOS == "windows":
+  const MAXIMUM_WAIT_OBJECTS = 64
+
   proc joinThread*[TArg](t: Thread[TArg]) {.inline.} =
     ## waits for the thread `t` to finish.
     discard waitForSingleObject(t.sys, -1'i32)
 
   proc joinThreads*[TArg](t: varargs[Thread[TArg]]) =
     ## waits for every thread in `t` to finish.
-    var a: array[0..255, SysThread]
-    sysAssert a.len >= t.len, "a.len >= t.len"
-    for i in 0..t.high: a[i] = t[i].sys
-    discard waitForMultipleObjects(t.len.int32,
-                                   cast[ptr SysThread](addr(a)), 1, -1)
+    var a: array[MAXIMUM_WAIT_OBJECTS, SysThread]
+    var k = 0
+    while k < len(t):
+      var count = min(len(t) - k, MAXIMUM_WAIT_OBJECTS)
+      for i in 0..(count - 1): a[i] = t[i + k].sys
+      discard waitForMultipleObjects(int32(count),
+                                     cast[ptr SysThread](addr(a)), 1, -1)
+      inc(k, MAXIMUM_WAIT_OBJECTS)
 
 else:
   proc joinThread*[TArg](t: Thread[TArg]) {.inline.} =