This works, check your basic program logic, it's wrong.
import locks, threadpool
var lock: Lock # declare to globals
initLock(lock)
template lockBlock(code: untyped): untyped =
# copy withLock implementation
acquire(lock)
try:
{.locks: [lock].}:
code
finally:
release(lock)
var val = 0
proc run() =
lockBlock:
var n = val
for i in ..10000:
n += 1 - 2*(i mod 2) # what is doing here does not mater
val = n
for i in 1..8: spawn run()
sync()
echo val # correct output should be 8
Run
- Lock code block without explicit lock variable ? jackhftang
- Re: Lock code block without explicit lock variable ? Araq
