How to set up shared data for multi-user simultaneous save and load?

2 visualizaciones (últimos 30 días)
Monica
Monica el 17 de Oct. de 2024
Comentada: Walter Roberson el 17 de Oct. de 2024
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?

Respuestas (1)

Walter Roberson
Walter Roberson el 17 de Oct. de 2024
Editada: Walter Roberson el 17 de Oct. de 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 comentarios
Monica
Monica el 17 de Oct. de 2024
So if I do a fopen() with append, fwrite() a single "block" as you describe, then fclose() and do a check to make sure the data saved correctly maybe? Once I verify the data is saved then I could do another loop through if I need to write something else.
Walter Roberson
Walter Roberson el 17 de Oct. de 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)

Iniciar sesión para comentar.

Productos


Versión

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by