I'm having issues using the parallelization helper as described here. Here is a basic reproduction:
import threadpool
{.experimental: "parallel".}
proc test() : bool =
for i in 1..100000:
discard "foobar"
return true
parallel:
let success = spawn test()
if success:
echo "success"
else:
echo "failure"
This always prints out "failure". It works if the loop is taken out, so maybe there's some sort of race condition. The non-parallel version also works fine:
proc test() : bool =
for i in 1..100000:
discard "foobar"
return true
let success = test()
if success:
echo "success"
else:
echo "failure"
I'm using Nim version 1.2.4. Any ideas on what's going on?
I think because spawn is non-blocking the if statement is evaluated before test is ever run, leaving success in its default state, which happens to be false.
You could test this by changing success into an int and setting any number other than 0 instead of true in test.
What should also work is taking the if statement out of the parallel block, as i assume all operations within the parallel block need to be finished before continuing.
import threadpool
{.experimental: "parallel".}
proc test() : bool =
for i in 1..100000:
discard "foobar"
return true
proc check_it(x: bool) =
if x:
echo "yes"
else:
echo "no"
proc aux() =
parallel:
let s = spawn test()
check_it(s)
aux()
Is this what you're trying to do?