import std/[locks, lists, strutils, os] var L: Lock proc threadFunc(interval: tuple[a,b: int]) {.thread.} = for i in interval.a..interval.b: acquire(L) # lock stdout echo i release(L) proc test() = var thr = newSeq[Thread[tuple[a,b: int]]](parseInt(paramStr(1))) # Dynamically allocate room for the threads initLock(L) for i in 0..thr.high: createThread(thr[i], threadFunc, (i*10, i*10+5)) # Create thread into already allocated memory for thread in thr.mitems: # mitems is used here because we don't want a copy (Nim will fail to compile without it) joinThread(thread) # Join all threads one by one deinitLock(L) # `thr` will be deallocated here, along with all the nodes test()