Logo

Programming-Idioms

History of Idiom 33 > diff from v12 to v13

Edit summary for version 13 by :

Version 12

2015-10-18, 13:19:52

Version 13

2015-10-29, 14:05:13

Idiom #33 Atomically read and update variable

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

Idiom #33 Atomically read and update variable

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

Imports
import Control.Concurrent.MVar
Imports
import Control.Concurrent.MVar
Code
putMVar x . f =<< takeMVar x
Code
putMVar x . f =<< takeMVar x
Doc URL
http://hackage.haskell.org/package/base/docs/Control-Concurrent-MVar.html
Doc URL
http://hackage.haskell.org/package/base/docs/Control-Concurrent-MVar.html
Imports
require 'atomic'
Imports
require 'atomic'
Code
x = Atomic.new(0)
x.update { |x| f(x) }
Code
x = Atomic.new(0)
x.update { |x| f(x) }
Doc URL
https://github.com/ruby-concurrency/atomic
Doc URL
https://github.com/ruby-concurrency/atomic
Origin
https://github.com/ruby-concurrency/atomic
Origin
https://github.com/ruby-concurrency/atomic
Code
let mut x = x.lock().unwrap();
*x = f(x);
Code
let mut x = x.lock().unwrap();
*x = f(x);
Comments bubble
Assuming x is created like this:
`let x = Mutex::new(0);`
Comments bubble
Assuming x is created like this:
`let x = Mutex::new(0);`
Doc URL
http://doc.rust-lang.org/std/sync/struct.Mutex.html#examples
Doc URL
http://doc.rust-lang.org/std/sync/struct.Mutex.html#examples
Demo URL
https://play.rust-lang.org/?code=use%20std%3A%3Async%3A%3AMutex%3B%0A%0Afn%20f(x%3A%20i32)%20-%3E%20i32%20%7B%0A%20%20%20%20x%20%2B%201%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20x%20%3D%20Mutex%3A%3Anew(0)%3B%0A%20%20%20%20let%20mut%20x%20%3D%20x.lock().unwrap()%3B%0A%20%20%20%20*x%20%3D%20f(*x)%3B%0A%20%20%20%20%0A%20%20%20%20println!(%22%7B%3A%3F%7D%22%2C%20*x)%3B%0A%7D%0A&version=stable
Demo URL
https://play.rust-lang.org/?code=use%20std%3A%3Async%3A%3AMutex%3B%0A%0Afn%20f(x%3A%20i32)%20-%3E%20i32%20%7B%0A%20%20%20%20x%20%2B%201%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20x%20%3D%20Mutex%3A%3Anew(0)%3B%0A%20%20%20%20let%20mut%20x%20%3D%20x.lock().unwrap()%3B%0A%20%20%20%20*x%20%3D%20f(*x)%3B%0A%20%20%20%20%0A%20%20%20%20println!(%22%7B%3A%3F%7D%22%2C%20*x)%3B%0A%7D%0A&version=stable
Imports
use threads;
use threads::shared;
Imports
use threads;
use threads::shared;
Code
my $x :shared;
$x = 0;

sub my_task {
   my $id = shift;
   for (1 .. 5) {
      sleep 2*rand();
      { # lock scope
         lock($x);
         print "thread $id found $x\n";
         $x = $id;
         sleep 2*rand();
      }
   }
}

threads->create('my_task', $_) for 1 .. 3;
sleep 5 while threads->list(threads::running);
Code
my $x :shared;
$x = 0;

sub my_task {
   my $id = shift;
   for (1 .. 5) {
      sleep 2*rand();
      { # lock scope
         lock($x);
         print "thread $id found $x\n";
         $x = $id;
         sleep 2*rand();
      }
   }
}

threads->create('my_task', $_) for 1 .. 3;
sleep 5 while threads->list(threads::running);
Comments bubble
lock is lexically scoped. Delay outside the lock scope allows other threads a chance at changing $x.
Comments bubble
lock is lexically scoped. Delay outside the lock scope allows other threads a chance at changing $x.