Last active
April 13, 2020 16:48
-
-
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`
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: 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