I'm running a multi threaded application on FC4 and I have trouble reading data from a file after writing to it.
I'll describe the scenario in more detail:
file.txt looks like this:
<start of file>
line1
line2
line3
<end of file>
Thread1:
fstream file;
//open file file.txt
file->open("file.txt", fstream::in | fstream::out);
//go to end of file
file->seekp(0, ifstream::end);
//write the string "line4" at eof
string str("line4");
file->write(str.c_str(), str.size());
//flush & close the file
file->flush();
file-> close();
....
*** after the string was written and the file got closed send a message to thread2 that it can read from the file ***
Thread2:
*** wait for message from Thread1 ***
*** after message received: open file, go to eof & read the last line -> error! the string shows "line3". "line4" was not written ***
--------------------------------------------------------
This problem occurs once in a while, so it's hard to catch it.
Also, it seems to me that I have no chance of catching it with a debugger before Thread2 fails reading line4, since the file will be flushed until I get to run the statement that reads from the file.
Anyone encountered problems like this in a multi threaded or even single threaded application?