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 = initSinglyLinkedList[Thread[tuple[a,b: int]]]() # Create a linked list to store our threads in initLock(L) for i in 0..parseInt(paramStr(1)): # Use dynamic number of threads let node = new SinglyLinkedNode[Thread[tuple[a,b: int]]] # Allocate a new node so we can create our thread into the node createThread(node.value, threadFunc, (i*10, i*10+5)) # Create thread into already allocated memory thr.add node 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()