Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save UNIcodeX/f8f055b7c5cf74e0003d7fc072432047 to your computer and use it in GitHub Desktop.
Save UNIcodeX/f8f055b7c5cf74e0003d7fc072432047 to your computer and use it in GitHub Desktop.
Example of parallel processing changing the value of a global variable without using a pointer. Instead it uses a `var` parameter, which automatically creates a pointer, as I understand it from Dom's book, Nim In Action.
import locks, threadpool, strutils
# compile with:
# nim c -r --threads:on -d:danger --gc:arc
{.experimental: "parallel".}
var
lock: Lock
a: seq[string]
initLock lock
proc process(p: var seq[string], s: string) =
withLock lock:
p.add(s)
when isMainModule:
parallel:
for i in 0 ..< 20:
spawn process(a, $i)
sync()
echo a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment