I have a {.thread.} proc that won't compile because it calls procs that access a global variable that the compiler thinks uses GC'd memory.
The variable is defined as:
const MAX_WORKERS = 10
type
Killer = object
lock: TLock
bailed {.guard: lock.}: bool
processes {.guard: lock.}: array[0..MAX_WORKERS-1, foreign ptr Process]
template hold(lock: TLock, body: stmt) =
lock.acquire
defer: lock.release
{.locks: [lock].}:
body
proc initKiller*(): Killer =
initLock(result.lock)
result.lock.hold:
result.bailed = false
for i, _ in result.processes:
result.processes[i] = nil
var killer = initKiller()
The idea is to kill off a bunch of external processes if an error occurs in one of them.
When I print the addresses of the global's fields (including array elements), they are all contiguous.
Where is the GC memory that the compiler is unhappy about, and how do I get around it? Even better, what are the guidelines for using globals as shared state between threads?