为什么os / exec.CombinedOutput()没有竞争条件?

时间:2017-03-31 10:36:47

标签: go thread-safety

Go bytes.Buffer isn't thread-safe。然而,当我阅读源代码时,我注意到os/exec.CombinedOutput()c.Stdoutc.Stderr使用相同的缓冲区。进一步阅读包的实现,在写入c.Stderr / c.Stdout here时看起来没有同步。

我是否遗漏了某些内容或者是否发现了可能的同步问题?子进程可以同时写入AFAIK stderr和stdout。

1 个答案:

答案 0 :(得分:2)

如果Stderr和Stdout与Cmd documentation中所述的作者相同,则不会同时写入:

  

如果Stdout和Stderr是同一个作家,一次最多只有一个goroutine会调用Write。

此功能在Cmd.stderr功能中实现。如果Stdout和Stderr相同,那么相同的fd将传递子进程stdout和stderr。如果fd是带有goroutine泵的管道,那么只有一个goroutine写给Stdout / Stderr。