I’m considering implementation of async logging. That means that several worker threads produce messages in memory and put them into a lock-free queue, and a single logging thread consumes this queue and performs blocking IO operations in background. The downside of this approach is that if any worker thread throws uncaught exception, the std::terminate
is called, and the logging thread may not finish its output. This is very undesirable, so I need to ensure that prior to program termination the queue would be emptied and all IO operations would be finished. For simplicity I’m considering the case when the logger itself may not cause the termination, and that termination is not caused by exception from static or thread local objects, that it is not caused by destruction of a joinable thread, etc.
What happens with other threads if one thread throws an uncaught exception? Can I yield to other threads from std::terminate
, block on mutex or implement busy waiting? I need to prevent adding new messages to logging queue letting the logger to empty it, and then I need to wait for the IO completion: should I busy wait on an atomic?
1