How to buffer stdout in memory and write it from a dedicated thread

Posted by NickB on Stack Overflow See other posts from Stack Overflow or by NickB
Published on 2009-06-05T13:52:14Z Indexed on 2010/04/26 12:33 UTC
Read the original article Hit count: 197

Filed under:
|
|
|

I have a C application with many worker threads. It is essential that these do not block so where the worker threads need to write to a file on disk, I have them write to a circular buffer in memory, and then have a dedicated thread for writing that buffer to disk.

The worker threads do not block any more. The dedicated thread can safely block while writing to disk without affecting the worker threads (it does not hold a lock while writing to disk). My memory buffer is tuned to be sufficiently large that the writer thread can keep up.

This all works great. My question is, how do I implement something similar for stdout?

I could macro printf() to write into a memory buffer, but I don't have control over all the code that might write to stdout (some of it is in third-party libraries).

Thoughts? NickB

© Stack Overflow or respective owner

Related posts about c

    Related posts about stdout