Setvar and Getvar Race Condition
Posted: Sun Jan 23, 2022 8:46 am
I have noticed when I set a number a variables (via exec.Setvar) and read them out later (via exec.Getvar) they are stale values. Seems like exec.Setvar is asynchronous and I have not found a way to block execution until the vars have been written; and no Thread.Sleep or exec.Wait is not acceptable answers as it is still not assurance/guarantee the write actually did finished with in some arbitrary time frame and yet still a race.
Even with a exec.IsMoving loop, there is still a chance (see `Counter: 23; #2000 Var:22`)
Test Macro
Even with a exec.IsMoving loop, there is still a chance (see `Counter: 23; #2000 Var:22`)
Test Macro
- Code: Select all
// Setvar and Getvar race condition
for(var i=0; i<10; i++){
Counter++;
exec.Setvar((double)Counter, 2000);
// while(exec.IsMoving()){}
var value = exec.Getvar(2000);
exec.AddStatusmessage("Counter: " + Counter + "; #2000 Var:" + value);
}
#Events
static public int Counter = 0;