var object = new Object() {
volatile int x;
};
new Thread(() -> {
synchronized (object) {
int x = object.x;
}
}).start();
synchronized (object) {
object.x = 123;
}
uses syncobjs;
var
loc: TCriticalSection;
begin
loc.Enter;
try
x := f(x);
finally
loc.Leave;
end;
end.
use threads;
use threads::shared;
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);
import threading
lock = threading.Lock()
lock.acquire()
try:
x = f(x)
finally:
lock.release()
var object = new Object() {
volatile int x;
};
new Thread(() -> {
synchronized (object) {
int x = object.x;
}
}).start();
synchronized (object) {
object.x = 123;
}