Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save UNIcodeX/b209b2118e3206acb96557d7d7b64310 to your computer and use it in GitHub Desktop.
Save UNIcodeX/b209b2118e3206acb96557d7d7b64310 to your computer and use it in GitHub Desktop.
This example of the parallel manipulation of a global var works without `--threadanalysis:off`
import locks, threadpool, strutils
# compile with:
# nim c -r --threads:on -d:danger --gc:arc
{.experimental: "parallel".}
var
lock: Lock
a: array[20, string]
initLock lock
# Can do this explicitly with:
# proc process(b: ptr array[20, string], s: string) =
# or lazily with:
proc process(b: ptr, s: string) =
withLock lock:
b[][parseInt(s)] = s
when isMainModule:
# could either explicitly define the pointer type
# var ptra: ptr array[20, string] = addr(a)
# or lazily with:
var ptra = addr(a)
parallel:
for i in 0 ..< a.len:
spawn process(ptra, $i)
sync()
echo a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment