I’d rather not recommend @spawn
to programmers new to threading. It’s a great foundation but difficult to use and loses clarity in the program. For example, while concise and self-contained, @foobar_lv2’s example contains subtly different but yet equivalent definitions of the “monoid” if res < be be, bp1, bp2 = res, pi, pj end
. There is no syntactic constraint that the required invariance between these two definitions is preserved after subsequent refactorings. Furthermore, when using @spawn
, you need to understand let
in let be = Inf64, bp1=-1, bp2=-1.0
is crucial (see PSA: Reasoning about scope rules and multithreading - #2 by tkf for why let
is needed; FYI, FLoops.jl tries to detect this bug).
If you want simple data parallelism like this, I strongly recommend learning how to use mapreduce (or, equivalently, something like FLoops.jl), at least as a first step.