Search code examples
cbuffer

In C, is flushing the output buffer asynchronous (fflush)?


If not, can it be made asynchronous so other code can be executed without having to wait for the flushing to be completed?


Solution

  • The Standard C function fflush(stream) causes any unwritten data for that stream to be delivered to the host environment to be written to the file. This occurs synchronously with respect to the current process, ie: fflush() does not return until the data has been delivered to the system, but the system may cache the file system so fflush() does not wait for the data to be written to the file system on disk or through the network.

    There is no standard function to make this happen asynchronously. You could conceivably use a separate thread to flush the stream buffer asynchronously, but fiddling with the same stream in different threads is not recommended.