Workaround with pointer to table and custom lock ¯_(ツ)_/¯ 
    
    
    import tables
    import strformat
    import os
    import typetraits
    import locks
    
    
    type
      ThreadName = string
      ThreadInfo = object
        thread: Thread[ThreadName]
    
    
    var threads = initTable[ThreadName, ThreadInfo]()
    let threadsPtr = threads.addr
    var threadsLock: Lock
    threadsLock.initLock()
    
    
    proc threadEntrypoint(name: ThreadName) {.thread.} =
      while true:
        echo &"Inside thread: name={name}"
    
    
    proc spawn(name: ThreadName) =
      echo &"Spawning: name={name}"
      
      withLock threadsLock:
        if name in threadsPtr[]:
          raise newException(ValueError, "Thread with this name was already 
registered")
        
        threadsPtr[][name] = ThreadInfo()
        threadsPtr[][name].thread.createThread(param=name, tp=threadEntrypoint)
        echo &"Created thread: name={name}"
    
    
    when isMainModule:
      spawn(name="a")
      spawn(name="b")
      
      sleep(1000)
    
    
    Run

Reply via email to