This commit is contained in:
Eugene Kabanov 2017-03-19 08:16:13 +02:00 • committed by Andreas Rumpf
commit e20af5cec6

View file

@ -465,17 +465,22 @@ proc handle*[TArg](t: Thread[TArg]): SysThread {.inline.} =
result = t.sys result = t.sys
when hostOS == "windows": when hostOS == "windows":
const MAXIMUM_WAIT_OBJECTS = 64
proc joinThread*[TArg](t: Thread[TArg]) {.inline.} = proc joinThread*[TArg](t: Thread[TArg]) {.inline.} =
## waits for the thread `t` to finish. ## waits for the thread `t` to finish.
discard waitForSingleObject(t.sys, -1'i32) discard waitForSingleObject(t.sys, -1'i32)
proc joinThreads*[TArg](t: varargs[Thread[TArg]]) = proc joinThreads*[TArg](t: varargs[Thread[TArg]]) =
## waits for every thread in `t` to finish. ## waits for every thread in `t` to finish.
var a: array[0..255, SysThread] var a: array[MAXIMUM_WAIT_OBJECTS, SysThread]
sysAssert a.len >= t.len, "a.len >= t.len" var k = 0
for i in 0..t.high: a[i] = t[i].sys while k < len(t):
discard waitForMultipleObjects(t.len.int32, 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) cast[ptr SysThread](addr(a)), 1, -1)
inc(k, MAXIMUM_WAIT_OBJECTS)
else: else:
proc joinThread*[TArg](t: Thread[TArg]) {.inline.} = proc joinThread*[TArg](t: Thread[TArg]) {.inline.} =