是 os. Chdir
、os.Setuid
和 os.Setgid
在 Go 中线程安全吗?
换句话说,如果我在两个并行运行的不同 goroutine 中执行 os.Chdir
(或其他两个函数),它会改变工作 goroutine 的目录还是整个过程(这会导致问题)?
我在文档中找不到任何信息。
最佳答案
在引擎盖下,os.Chdir
只是 calls chdir()
系统调用:
211 func Chdir(dir string) error {
212 if e := syscall.Chdir(dir); e != nil {
213 return &PathError{"chdir", dir, e}
214 }
215 return nil
216 }
所以它影响了整个过程。 os.Setuid
和 os.Setgid
也是如此。
同时从多个 goroutine 调用它们可能是安全的,因为执行系统调用会暂停调度程序;但要注意竞争条件。
关于multithreading - Chdir 和Setuid 以及Setgid 线程安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23180000/