So, i'm working on a small multi-threaded game server. Currently, there is a finite amount of threads and each of them are taking care of different tasks. So, the whole loop is naturally thread-safe. Unfortunately, there are a few parts of the application that threads need to share. Logging, for example. I have an unordered_map<string, ostringstream> static variable that i use for logging stuff dynamically. At the end of each loop iteration, i have condition to check the size of the ostringstream, and if it's higher than X, i save it to disk. This way, i'm only offloading logs to disk a few times, not on every frame.
The problem is: when the loop is idle, waiting for commands, the log can be "stuck". If the ostringstream is lower than X, and i go to an idle state, then it keeps there forever, never offloading to disk the last parts of the log. Right now, i've made another thread that runs on intervals, responsible for checking if the size of the buffer has changed or not. If it hasn't changed for a long, it dumps it.
But is there a better way of handling this?