Created
April 13, 2020 16:13
-
-
Save UNIcodeX/a184506252ce3c8767ece47188e71e21 to your computer and use it in GitHub Desktop.
Example of parallel processing NOT using pointers. NOTE: Sometimes one or more values does not get properly set in the array.
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 | |
| {.experimental: "parallel".} | |
| var | |
| lock: Lock | |
| a {.guard: lock.}: array[20, string] | |
| initLock lock | |
| proc process(s: string): string = | |
| withLock lock: | |
| return s | |
| when isMainModule: | |
| parallel: | |
| for i in 0 ..< a.len: | |
| a[i] = ^ spawn process($i) | |
| sync() | |
| echo a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment