You could not use the global GC value of "w" in a thread. So you have to make
that const first of all.
Then going back to "basic" coding. This works:
# nim c --threads:on -d:release
{.experimental.}
import times
import threadpool
const w = ["aaaaaaaaaa", "bbbbbbbbbb","cccccccccc"]
template parallelFor(
slice: Slice[int],
f:expr
) =
const a = slice.a
const b = slice.b
parallel:
for i in a..b: spawn f(i)
proc doI(i: int) = echo w[i]
parallelFor(0..2, doI)
as does this (do syntax):
# nim c --threads:on -d:release --verbosity:0
{.experimental.}
import times
import threadpool
const w = ["aaaaaaaaaa", "bbbbbbbbbb","cccccccccc"]
template parallelFor(
slice: Slice[int],
f:expr
) =
const a = slice.a
const b = slice.b
parallel:
for i in a..b: spawn f(i)
parallelFor(0..2) do(i: int) {.closure.}: echo w[i]
Not sure if that is a compiler bug or limit to the `=>` syntax but I can't find
where to put the pragma.