C/C++ file output buffering/buffer handover to the OS

146 views Asked by At

I'm maintaining a single-threaded C++ app that has to write a large binary file from time to time. I'd like to minimize the time spent in { stream(fname); stream.write(largebuf}; }, so hand over the data to the OS (Linux) as fast as possible when writing/closing the stream. I'm not really concerned about when the data is actually written physically to the disk.

This could wery well be an OS tuning question (any pointers are welcome) - I'd just like to know if there's anything I can do within the C++ code to improve turnaround time from the block shown above.

thanks, T.

2

There are 2 answers

0
Ivan Genchev On

Is there any particular reason you can not use threads? If you don't care when the data is actually written to the disk you can just use threads so you don't block your main thread while writing that large bin file.

std::thread write_thread([&fname, &largebuf](){ stream(fname); stream.write(largebuf}; });

PS: the sample is using C++11 features (threads and lambda functions).

0
Julien B. On

You may consider using std::async if building C++11 is not an issue.