summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorEugene Kabanov <ka@hardcore.kiev.ua>2017-03-19 08:16:13 +0200
committerAndreas Rumpf <rumpf_a@web.de>2017-03-19 07:16:13 +0100
commite20af5cec68520a7ed96a6fd130af2fa56f6709b (patch)
tree8922f364aaa1271ea55f8917efffc44006da493b /lib
parent156bd29c68a1eaf03a9211475d704ac510169287 (diff)
downloadNim-e20af5cec68520a7ed96a6fd130af2fa56f6709b.tar.gz
Fix #4972. (#5567)
Diffstat (limited to 'lib')
-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.} =