Maybe something like this? I haven't played much with threads in Nim yet: 
[https://github.com/nim-lang/Nim/blob/4f062c3be08fa2bc3e167e1a6b9842c92bc8c8f7/tests/threads/tonthreadcreation.nim](https://github.com/nim-lang/Nim/blob/4f062c3be08fa2bc3e167e1a6b9842c92bc8c8f7/tests/threads/tonthreadcreation.nim)
    
    
    import locks
    
    var
      thr: array[0..3, Thread[int]]
      L: Lock
      top  {.threadvar.}: seq[string]
    
    proc threadDied() {.gcsafe.} =
      echo "Dying: ", top
    
    proc foo(i: int) {.thread.} =
      onThreadDestruction threadDied
      
      {.gcsafe.}:
        top = @["hello", "world"]
      
      acquire(L)
      echo "Top in thread ", i, ": ", top
      release(L)
    
    proc main =
      initLock(L)
      for i in 0..high(thr):
        createThread(thr[i], foo, i)
      
      joinThreads(thr)
    
    main()
    

Seems to work on my machine (Windows) for that very basic example.

[In the stdlib, 
`onThreadCreation`](https://github.com/nim-lang/Nim/blob/4f062c3be08fa2bc3e167e1a6b9842c92bc8c8f7/lib/pure/httpclient.nim#L342)
 is used, but [it seems to have been removed from 
`devel`](https://github.com/nim-lang/Nim/commit/4f062c3be08fa2bc3e167e1a6b9842c92bc8c8f7)

Reply via email to