重定向每个线程的stdout / stderr [英] Redirect stdout/stderr for each thread

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

问题描述

我喜欢将每个线程的stdout的输出重定向到一个文件。以下代码将所有线程输出重定向到单个文件 -

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下一个线程可以可靠地将其stdout映射到文件名_threadid?

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

推荐答案

在所有平台上,标准流(stdin,stdout,stderr)是每个进程。因此,它们不能被每个线程重定向。你应该修改你的代码,使每个线程输出到一个特定的文件,而不是stdout。

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.

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

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