import ballerina/io;
type R record {
int v;
};
// The initialization expression of an `isolated` variable
// has to be an `isolated` expression, which itself will be
// an `isolated` root.
isolated R r = {v: 0};
isolated function setGlobal(int n) {
// An `isolated` variable can be accessed within
// a `lock` statement.
lock {
r.v = n;
}
}
public function main() {
setGlobal(200);
// Accesses the `isolated` variable within a
// `lock` statement.
lock {
io:println(r);
}
}
Combining isolated functions and lockCombining
|
import ballerina/io;
type R record {
int v;
};
isolated R r = {v: 0};
The initialization expression of an isolated
variable
has to be an isolated
expression, which itself will be
an isolated
root.
isolated function setGlobal(int n) {
lock {
r.v = n;
}
An isolated
variable can be accessed within
a lock
statement.
}
public function main() {
setGlobal(200);
lock {
io:println(r);
}
Accesses the isolated
variable within a
lock
statement.
}
bal run combining_isolated_functions_and_lock.bal
{"v":200}