Proper output buffering algorithm

Started by
1 comment, last by vinnyvicious 7 years, 11 months ago

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?

Advertisement

Can you make the loop's idle state have a timeout - so while waiting for commands, it will also wake up on it's own if no command is received within a certain amount of time?

The loop is based on libev, so i tried the timeout feature, but it didn't work very well. Timeouts were never triggered because, well, there was no timeout going on. I've also tried libev's idle callback, but it runs too many times, to the point where my conditional for the size of the buffer was affecting performance.

This topic is closed to new replies.

Advertisement