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

后端 未结 7 472
既然无缘
既然无缘 2020-11-29 03:18

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

7条回答
  •  情书的邮戳
    2020-11-29 03:38

    You can "redirect" stdout into file using freopen().

    man freopen says:

    The freopen() function opens the file whose name is the string pointed to by path and associates the stream pointed to by stream with it. The original stream (if it exists) is closed. The mode argument is used just as in the fopen() function. The primary use of the freopen() function is to change the file associated with a standard text stream (stderr, stdin, or stdout).

    This file well could be a pipe - worker threads will write to that pipe and writer thread will listen.

提交回复
热议问题