I apologize if this is a duplicate question.
OS/Language:
- C++ 11/14
- Linux (Rocky 4.18.0-425.3.1.el8.x86_64)
- libc.so.6
Use Case:
Application has two threads, one writes ~7K data (text appended with a new line) to a file till 50MB size is reached. Another thread reads the same file and processes each record, while the other thread continues writing. Each thread opens a file handle (i.e. its not shared). Both write and read thread opens and closes the file only once. Code snippet below for reference (just to capture the idea, not for production):
Thread 1 calling...
void writeToFileInBlocks()
{
...
std::ofstream out;
std::string bufferfile = "/mymount/demo.txt";
out.open(bufferfile.c_str(), std::fstream::app);
while (1)
{
if (out.is_open())
{
out << msg;
fsync(handle);
// Reached 50MB
if (counter >= iter) break;
} else {
std::cout << "Failed to openn";
}
}
out.close();
}
Thread 2 calling...
void readFromFile()
{
...
std::string filePath = "/mymount/demo.txt";
std::ifstream currentSegment;
currentSegment.open(filePath.c_str());
while (1)
{
if (currentSegment.is_open())
{
currentSegment.seekg(currentPos, std::ios::beg);
for (int i=0; i< batchsize; ++i)
{
std::string line;
getline(currentSegment, line, 'n');
...
}
currentPos = currentSegment.tellg();
if (currentSegment.eof()) break;
}
else {
std::cout << "File open failed during readn";
exit(1);
}
}
currentSegment.close();
...
}
Question:
Any potential issue that might occur while reading in terms of:
a. File not closed by either of the threads till they finished the processing.
b. Can there be loss of data
Abhijit Dutta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4