重定向标准输出/标准错误的每个线程 [英] Redirect stdout/stderr for each thread

查看:207
本文介绍了重定向标准输出/标准错误的每个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢重定向从标准输出输出为每个线程到文件中。下面code所有线程输出重定向到一个文件 -

I like to redirect output from stdout for each thread to a file. The following code redirect all thread output to a single file -

int fd = open(<filename_threadid.txt>, <flags>)
_dup2(fd, 1)

我应该如何恢复原来的stdout所以接下来线程可以标准输出可靠地映射到filename_threadid?

How should I restore the original stdout so the next thread can reliably map its stdout to the filename_threadid?

推荐答案

在所有平台上的标准流(标准输入,标准输出,标准错误)每过程。因此,它们不能在每个线程被重定向。你应该修改code,使每个线程输出到一个特定的文件,而不是标准输出。

On all platforms the standard streams (stdin, stdout, stderr) are per process. As such they cannot be redirected per thread. You should modify your code so that each thread outputs to a specific file instead of the stdout.

这篇关于重定向标准输出/标准错误的每个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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