I want to use multi-threading (with @spawn/@thread
) in a static scheduling to prevent threads jumping during the time-loop in this MWE, nthreads()=2
here:
using .Threads
function f_spawn()
thrs = [Threads.@spawn begin
for t in 1:3
if threadid() == 1
println("Iteration $t -First Section -Hi from $(threadid())")
end
println("Iteration $t -Execution Section -Hi from $(threadid())")
end
end for i = 1:nthreads()];
fetch.(thrs);
end
f_spawn()
I expect the results to be:
Iteration 1 -First Section -Hi from 1
Iteration 1 -Execution Section -Hi from 1
Iteration 1 -Execution Section -Hi from 2
Iteration 2 -First Section -Hi from 1
Iteration 2 -Execution Section -Hi from 1
Iteration 2 -Execution Section -Hi from 2
Iteration 3 -First Section -Hi from 1
Iteration 3 -Execution Section -Hi from 1
Iteration 3 -Execution Section -Hi from 2
However, they are:
Iteration 1 -Execution Section -Hi from 2
Iteration 1 -Execution Section -Hi from 2
Iteration 2 -Execution Section -Hi from 2
Iteration 2 -First Section -Hi from 1
Iteration 2 -Execution Section -Hi from 1
Iteration 3 -Execution Section -Hi from 2
Iteration 3 -First Section -Hi from 1
Iteration 3 -Execution Section -Hi from 2