|
|
|
|
|
by cogman10
823 days ago
|
|
There is no problem here with the critical region. The problem would be removing the critical region because "there's just one thread". This is incorrect code a = foo();
b = io(a).await;
c = bar(b);
Without the lock, `a` can mutate before `b` is done executing which can mess with whether or not `c` is correct. The problem is if you have 2 independent variables that need to be updated in tandem.Where this might show up. Imagine you have 2 elements on the screen, a span which indicates the contents and a div with the contents. If your code looks like this mySpan.innerText = "Loading ${foo}";
myDiv.innerText = load(foo).await;
mySpan.innerText = "";
You now have incorrect code if 2 concurrent loads happen. It could be the original foo, it could be a second foo. There's no way to correctly determine what the content of `myDiv` is from an end user perspective as it depends entirely on what finished last and when. You don't even know if loading is still happening. |
|
But personally I wouldn’t solve it with a lock. I’d solve it by making the state machine more explicit and giving it a little bit of distance from the view logic. If you don’t want multiple loads to happen at once, add an is_loading variable or something to track the loading state. When in the loading state, ignore subsequent load operations.