Multithreading in Julia



a = Threads.Atomic{Int}(0);
b = zeros(10)
Threads.@threads for i = 1:10
 a[] += i
 b[i] = Threads.threadid()

When running this code with multiple threads , the value of variable “a” at the end of every iteration is different, certainly less than 55 though. I am unable to understand how exactly is Atomic working here.


(Please quote your code)

Atomic variable does not mean multiple modification on them will be magically atomic, it only means that every single operations on them are atomic and also obey certain ordering guarantees. In this case a[] += i is not a single operation, it is an atomic load, an add, and an atomic store. If you need atomic read-modify-write (RMW), you must use the corresponding functions in Threads. (In this case I believe it’s Threads.atomic_add!()).

Also note that this code will be slower than single threaded version by a huge factor. (So only good for testing).


Thanks, I got this working


A post was split to a new topic: Global variables as thread local storage