Reputation: 460
Is there something in Julia Threads similar to a single
command in OpenMP that will ensure all threads wait before a particular block of code and then execute that block in only one thread? I have a loop that distributes calculations of forces between threads before performing an update to all locations at once, and I cannot find any feature to achieve this without terminating the @threads
loop.
Upvotes: 2
Views: 950
Reputation: 42244
You can use locks:
function f()
l = Threads.SpinLock()
x = 0
Threads.@threads for i in 1:10^7
Threads.lock(l)
x += 1 # this block is executed only in one thread
Threads.unlock(l)
end
return x
end
Note that the SpinLock
mechanism is dedicated to non-blocking codes (that is computations only, no I/O in the loop). If there is I/O involved ReentrantLock
should be used instead.
Upvotes: 5