I have a function that optionally uses threads for its main loop, doing so when an argument usingthreads
is true
. At the moment, the code looks like this:
function dosomething(usingthreads::Bool)
n = 1000
if usingthreads
Threads.@threads for i = 1:n
#20 lines of code here
end
else
for i = 1:n
#same 20 lines of code repeated here
end
end
end
Less nasty than the above would be to put the "20 lines" in a separate function. Is there another way?
julia function dosomething(::Val{false}) # nonthreaded case # implementation here end function dosomething(::Val{true}) # nonthreaded case # threaded implementation here end dosomething(usethreads::Bool) = dosomething(Val(usethreads))
Would this be good style? – Ekaterinoslav