One thread is only writing the file and other one is only reading that writen file.
Thread that is writing the file will be opening file with wb (write binary) flags.
It only overwrites first 4 bytes which is int and shows how many elements
there are in file. It adds new data at the end of the file and after that it
will overwrite first 4 bytes ( updating elements count )
Thread what is reading the file will be opening file with rb ( read binary ) flags. That thread can read the file while other thread is writing it.
It should not read from the part what is currently being writen by other thread
since element count variable will be updated after element(s) is successfully writen.
Thread what is either writing or reading can be part of another program.
Programs can be either 32bit or 64bit.
Programs run on same oc and on same computer.
There will be always only one thread/program writing the file but
file could be read by many threads/programs at the same time.
My question is, would my plan be safe?
Would threads/programs, which are reading the file, get always correct/uncorrupted data?
The readers are not guaranteed to get uncorrupted data while the writer is writing to the file.
You should keep all readers from reading while the file is being written to. Avoid starvation so that the readers aren't reading the file constantly and preventing the writer from writing to the file.
This is the classes readers-writers problem, and a google search can find you many solutions, many of which include semaphores, but all of which will include some method of keeping either the reader or writer locked out while the other is doing its job.