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 = newSeqOfCap[Thread[tuple[a,b: int]]](2) initLock(L) for i in 0..parseInt(paramStr(1)): # Use dynamic number of threads thr.setLen thr.len + 1 createThread(thr[thr.high], 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()