Thanks for testing it!
I tried different combinations for writing thread instance to sharedTable.
Seems that creating temporary variable may help, but other cases simply don't
work:
import sharedtables
import strformat
import os
import typetraits
type ThreadInfo = object
thread: Thread[void]
var threads: SharedTable[string, ThreadInfo]
threads.init()
proc threadEntrypoint() {.thread.} =
while true:
echo "OK"
when isMainModule:
let key = "key"
# 1) works (well, if it didn't work nim would worth nothing)
var value = ThreadInfo()
value.thread.createThread(tp=threadEntrypoint)
# 2) illegal storage access
threads.withKey(key) do (key: string, value: var ThreadInfo, pairExists:
var bool):
if pairExists:
raise newException(ValueError, "Thread with this name was already
registered")
value = ThreadInfo()
value.thread.createThread(tp=threadEntrypoint)
pairExists = true
# 3) works
threads.withValue(key, value) do:
echo "Value already exists"
do:
var info = ThreadInfo()
info.thread.createThread(tp=threadEntrypoint)
threads[key] = info
# 4) hangs forever
threads.withValue(key, _) do:
echo "Value already exists"
do:
threads[key] = ThreadInfo()
threads.mget(key).thread.createThread(tp=threadEntrypoint)
sleep(1000)
Run