I just realised that I was running two c++ programs at the same time accessing the same file (one writing and one reading). I'm not positive they are actually "in" the file at the exact same moment (or even if a read/write object or what they're called in the respective programs have the file open simultaneously) but it's a good chance they are.
The writing function is using
1 2
ofstream out;
out.open("data.dat", ios::app);
and the reading function is using
1 2
ifstream in;
in.open("data.dat")
So my question is: what are the consequences of this? It seems to work nicely but I get a bit worried. If for example the write function tries to append the file at the same time as the read function reach the bottom of the file, what happens? If this unlucky coincidence doesn't occur am I ok?
I've noticed some of microsofts File classes having options for shared reading and writing. To me, this is somewhat indicative of differences in file handling operations on a concurrent level.
Can you jippo your two c++ programs such that you force the two to actually access the file simulataneously - basically hold open your write lock and then try running the program which tries to acquire the read lock. If this fails to open and you handle the exception correctly in your code then I think you should be safe.
Alternatively if you are using the file purely for communication between the two c++ programs and want to continue using a file system then you might also want to consider the following:
Send "msg" files to reading program by creating a date-time and sequence number stamped file in a particular folder. When you create these files, first create them in some tempory folder, then rename to real folder (should minimise concurreny overhead). Your read program should then scan this folder for new files, read it and then delete. This should then relieve certain contention.