I need my code (C++, on linux) to call a second executable, having previously written an output file which is read by the second program. Does the naïve approach,
std::ofstream out("myfile.txt");
// write output here
out.close();
system("secondprogram myfile.txt");
suffer from a potential race condition, where even though out.close() has executed, the file cannot immediately be read by secondprogram
? If so, what is the best practice for resolving this?
Three notes:
- If this is file-system-dependent, I'm interested in the behaviour on ext3 and tmpfs.
- Clearly there are other reasons (file permissions etc.) why the second program might fail to open the file; I'm just interested in the potential for a race condition.
- The hardcoded filename in the example above is for simplicity; in reality I use
mkstemp
.