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

标签 go thread-safety

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

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

最佳答案

如果 Stderr 和 Stdout 是 Cmd documentation 中所述的同一作者,则它们不会同时写入。 :

If Stdout and Stderr are the same writer, at most one goroutine at a time will call Write.

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

关于go - 为什么 os/exec.CombinedOutput() 没有竞争条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43138596/

相关文章:

go - 可测试的数据库/sql sql.Rows in go

google-app-engine - Google App Engine Golang 返回 404 页面未找到

python - PyQT线程的最简单方法

java - Hazelcast IMap put 方法获取存储桶、分区或 key 上的锁?

c++ - C++ 中多线程的安全性与速度

go - 如何打印链表

go - 将整数 slice append 到整数 slice 的 slice 会修改被 append 的 slice

http - Golang 反向代理以避免 SOP

python - subprocess.Popen 不是线程安全的?

java - ObjectMapper - 线程安全和性能的最佳实践