I have a critical process running callbacks on a main thread, and I log various data to files from those callbacks. I don't want to incur the performance penalty of flushing the output to file during critical callbacks, but I also can't wait indefinitely for the system to decide to flush the ofstream buffers. I.e., I can wait a few seconds, but not minutes, to see the data in my file monitors.
Can I just run a timer on a separate thread that frequently flushes the ofstreams, or is that not thread-safe?
Or is there a better way to log data to files that minimizes the load on the critical thread?
(I use Boost if that offers any convenient solutions.)