I’ve been using Julia to parallelize some algorithms, so I’m also studying how julia parallel programming works.
I did a simple code to check the behavior of Threads.@spawn and I’ve noticed it would only use the master thread the first time I called the function with the macro, if I kept calling it, it would not use the master thread anymore.
Could someone explain this behavior to me?
This is the code I used is this one. The call_threads is the array I count how many times the thread is invoked, I noticed it stores a value the first time I call the function, but the value that corresponds to the first thread doesn’t change in the following calls.
max_thread = zeros(Threads.nthreads()) call_threads = zeros(Threads.nthreads()) function generatingNumber() candidate = (rand() * 20) if (candidate > max_thread[Threads.threadid()]) max_thread[Threads.threadid()] = candidate end call_threads[Threads.threadid()] +=1 end function runthreads() global max_thread thread = Task for i in 1:100 push!(thread, Threads.@spawn generatingNumber()) end for i in 1:length(thread) wait(thread[i]) end max_value = maximum(max_thread) max_thread = zeros(Threads.nthreads()) end