After calling fclose (), how fast will the file be available for another thread?

I have an application running on Windows and Linux. It creates a file, writes to it, and calls fclose (). After some indefinite time, the file name is sent by another stream. Another thread opens the file using the fopen () function and reads its contents.

One user reports that the application does not work on Debian 6 if it does not create a short delay before opening the file.

When fclose () is called and returned, how fast will the file be available to other threads through fopen ()?

+4
source share
2 answers

Your question is a bit ambiguous, so I will try to answer a few interpretations.

If your question is how soon after fclose it is possible to return to the fopen file again, the answer will be simple that it is possible at any time, even before fclose . POSIX does not allow opening a file to be mutually exclusive; it can open the file as many times as you like if you do not fall within the limits of open system files. Even if you are on an inappropriate platform, such as Windows, with the implementation of pseudo-POSIX threads while fopen ordered after fclose , you are fine, because the basic close operation must complete before fclose can return.

If your concern is the availability of data written by one thread for reading by another thread, then as long as you can establish a “between events” connection before the flash (either explicitly via fflush or as part of the fclose operation) that wrote data and reads into elsewhere, everything is fine. Any pthread synchronization functions, such as using a mutex or pthread_join , would be sufficient to establish this relationship.

+3
source

fclose () is provided by libc, so the implementation may vary on different platforms / OS, and I doubt there is a way to detect file availability after fclose () is started and returned. Perhaps you can configure the delay setting and configure it for different clients.

-1
source

Source: https://habr.com/ru/post/1486076/


All Articles