Logo

Programming-Idioms

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

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.

$mutex = Mutex::create();
Mutex::lock($mutex);
$x = f($x);
Mutex::unlock($mutex);
Mutex::destroy($mutex);

Warning
The Mutex class has been removed in pthreads v3.
(def x (atom 0))
(def f inc)
(swap! x f)

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