Created
April 15, 2020 04:13
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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