如何缓冲标准输出在内存中,它从一个专门的线程写 [英] How to buffer stdout in memory and write it from a dedicated thread

查看:94
本文介绍了如何缓冲标准输出在内存中,它从一个专门的线程写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多工作线程C应用程序。至关重要的是,这些并不能阻止这样的地方工作线程需要写入磁盘上的文件,我让他们写在内存中的循环缓冲区,然后有一个专门的线程编写中buffer to disk。

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?

我可以宏观的printf()写入到内存缓冲区,但我不拥有控制权的所有code可能写入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).

的思考?
NickB

Thoughts? NickB

推荐答案

我喜欢用的想法则freopen 。您可能还可以重定向标准输出来管使用 DUP dup2 ,然后用从管道中获取数据。

I like the idea of using freopen. You might also be able to redirect stdout to a pipe using dup and dup2, and then use read to grab data from the pipe.

东西像这样:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define MAX_LEN 40

int main( int argc, char *argv[] ) {
  char buffer[MAX_LEN+1] = {0};
  int out_pipe[2];
  int saved_stdout;

  saved_stdout = dup(STDOUT_FILENO);  /* save stdout for display later */

  if( pipe(out_pipe) != 0 ) {          /* make a pipe */
    exit(1);
  }

  dup2(out_pipe[1], STDOUT_FILENO);   /* redirect stdout to the pipe */
  close(out_pipe[1]);

  /* anything sent to printf should now go down the pipe */
  printf("ceci n'est pas une pipe");
  fflush(stdout);

  read(out_pipe[0], buffer, MAX_LEN); /* read from pipe into buffer */

  dup2(saved_stdout, STDOUT_FILENO);  /* reconnect stdout for testing */
  printf("read: %s\n", buffer);

  return 0;
}

这篇关于如何缓冲标准输出在内存中,它从一个专门的线程写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆