How to set up shared data for multi-user simultaneous save and load?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a program I want to share with other users. The program accesses a central data file, loads in the data, and saves any new data. I haven't tested anything with multiple users; I'm worried that if two people hit "save" at the same time that data will be overwritten. I'm thinking that MATLAB Drive might help with this. Does anyone have any suggestions for how to set it up?
For some more detail to help answering, consider a reddit-style discussion board using MATLAB AppDesigner. If I'm writing a comment on one discussion question and a colleague is wrigint another comment, and we both hit "send" or "save", how can I ensure that data isn't lost?
0 commentaires
Réponses (1)
Walter Roberson
le 17 Oct 2024
Modifié(e) : Walter Roberson
le 17 Oct 2024
Simultaneous access is a true problem that you are right to be concerned about.
One trick:
If you fopen() with "append" mode, and you fwrite() a single block of data, then everything you write in one call to fwrite() will be forced to be written atomically at the current end of file. This includes the possibility of another process having "slipped in" and written to the end of file: the circumstance of append mode and fwrite() a single block will do an internal move-to-end-of-file as part of writing the data out.
Note that this only applies to single fwrite() calls: if you have two fwrite() calls in a row, then another process can sneak in between the two calls. So you may need to construct internal buffers of aggregate information and then fwrite() the internal buffer.
2 commentaires
Walter Roberson
le 17 Oct 2024
fopen() with 'a+' permission. You will be able to fread() and fseek() as necessary to read existing data. Every fwrite() will be forced to end-of-file.
You probably do not need to keep fopen() and fclose()
What you probably will need to do is ftell() immediately after the fwrite() in order to detect where in the file you ended up. If you record the output of fwrite() then that is the count of the number of items transferred; multiply that by the size of each element written and fseek backwards by that amount to get to the beginning of where you wrote the data. (If you are writing uint8() then the size of element written will be 1)
Voir également
Catégories
En savoir plus sur Matrox Hardware dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!