Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Fortran

Idiom #33 Atomically read and update variable

Assign to the variable x the new value f(x), making sure that no other thread may modify x between the read and the write.

x = f(x)

This is for a normal variable. In Fortran, normal variables which are not coarrays cannot be changed by another image.
integer, dimension[*] :: x

critical
  x = f(x)
end critical

This is for a coarray scalar variable, which could be changed by another image.
(def x (atom 0))
(def f inc)
(swap! x f)

New implementation...
< >
programming-idioms.org