3 ms·
You say that "if two tasks try to access a variable concurrently it gets marked atomic and they contend for access." This is actually not sufficient. As victor
by eveningcoffee 11y ago
You say that "if two tasks try to access a variable concurrently it gets marked atomic and they contend for access."
This is actually not sufficient. As victorNicollet pointed out, JS gives very strong guaranties when executing the code.
Let say
function swap(a, b) {
var swap;
swap = a.amount;
a.amount = b.amount;
b.amount = swap.
}
If you just mark a and b atomic, the code above will fail. You have to run it in transaction (see software transactional memory) or use locks.
If you use STM, then you can abstract away locks and have control over the lock taking order so you can avoid dead locks.
For example Clojure language is using STM, but they assume that the variables used in transaction are immutable, as they only manage the control over the references.