import std/[strformat]
import std/[os]
import std/typedthreads
import std/[monotimes, times]
var threads: seq[Thread[void]] = @[]
let st = getMonoTime()
proc doSleep() =
sleep 3000
for i in 0 ..< 10:
var t: Thread[void]
createThread(t, doSleep)
threads.add t
for t in threads.items:
t.joinThread
let ed = getMonoTime()
echo fmt"elapsed: {(ed-st).inMilliseconds}ms"
Error: '=dup' is not available for type <Thread[system.void]>, which is infe
rred from unavailable '=copy'; requires a copy because it's not the last read of 't'; routine: thread
If the number of thread instances required is unknown, how can we define it in a global variable?
A Thread cannot be copied only moved. It's a resource.
My guess is that the Thread object is allocated on the stack, but does not support copy, which means that its lifetime cannot be safely expanded.
Not, it just cannot be copied, you have to go through the OS to create/destroy it.