Logo

Programming-Idioms

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

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.

synchronized x = f(x);

The synchronized statement can also be written with braces, but for one liners, they can be omitted.

The synchronized statement automatically wraps what ever is in it in a mutex.
(def x (atom 0))
(def f inc)
(swap! x f)

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