05-24-2024, 02:10 AM
(05-24-2024, 12:56 AM)SMcNeill Wrote: You can skip that delay completely and the flag system will still work. The programs just take turns working with the file.The issue is writefile, both programs call it when the status is zero. Imagine this flow:
Even with caching, it wouldn't matter, as the control remains with the other program until that cache catches up and updates... though if your cache doesn't push to file for a while, you may end up with both program just being locked in a waiting state until that write finally occurs.
Program 1 calls Checkfile, sees zero. Calls writefile
Program 2 calls checkFile, sees zero. Calls writefile
Program 2 calls Put #1 with 22
Program 2 calls Get #1, sees 22, continues
Program 1 calls Put #1 with 12
Program 1 calls Get #1, sees 12, continues
At this point, both programs have continued in writefile and are writing to the file at the same time. The delay effectively prevents this situation simply because the actual code in writefile takes less than .1 seconds to finish writing to the file. Even when both programs enter writefile, the delay means program 1 can't actually start writing until well after program 2 is done.
Caching can be a problem in the same situation, the program might not reread the status code from the file when you do the `Get`. In that situation the order doesn't matter, if they both call `writefile` then they both write to the file because they don't see eachother's status updates (and then like you said, get locked up as they won't see the status to read).