Thank you @QMaster! That was it, here's the updated example for reference: # nim c -r --threads:on -d:debug example.nim import tables, locks, threadpool type Foo = object of RootObj lock: Lock data {.guard: lock.}: Table[string, int] FooPtr = ptr Foo proc init(f: Foo): FooPtr = var fp = cast[FooPtr](allocShared0(sizeof(Foo))) fp.lock.initLock() fp.lock.withLock: fp.data = initTable[string, int]() return fp proc read(fp: FooPtr, k: string): int {.thread.} = var f = cast[FooPtr](fp) f.lock.withLock: return (if f.data.hasKey(k): f.data[k] else: -1) proc write(fp: FooPtr, k: string, v: int): void {.thread.} = var f = cast[FooPtr](fp) f.lock.withLock: f.data[k] = v proc main(): void = let foo = Foo().init() defer: deallocShared(foo) for i in 0..1000: spawn foo.write("foo", i) discard spawn foo.read("foo") echo foo.read("foo") main() Run
Being new to Nim, which I must say is quite an aswesome language, is the code above idiomatic or is there a safer/simpler way to achieve what I'm trying to do?