Hi, What are the implications of using a "thread-local heap" garbage collector (e.g. --gc:refc) when I am spawning new threads? For example, is this still allowed:
func doStuff(data: ptr seq[Type]) =
...
var data: seq[Type] = ...
spawn doStuff(addr data)
@tsojtsoj:
Yes, spawning as per your code is still fine (as long as you use --threads:on in your compiling command line and import threadpool) but you should make sure the object pointed to doesn't get collected due to no reference, as per the following working code:
# compile with `--threads:on`...
import threadpool
func doStuff(data: ptr seq[int]) =
{.cast(nosideeffect).}:
echo data[]
proc main() =
var data = @[ 1, 2, 3, 4 ]
data.GC_ref
spawn doStuff(data.addr)
data.GC_unref
sync()
if isMainModule: main()
@tsojtsoj:
It might not be necessary, but it is implementation dependent: If the only use of data is to pass it to the spawned thread, then it will be moved to the thread and doesn't need to exist after that point, thus could be subject to garbage collection when triggered.