可以在不同的工作目录中拥有两个不同的goroutine吗?

huangapple go评论90阅读模式
英文:

Is it possible to have two different goroutines with different working directories?

问题

我想知道是否可以实例化两个不同的Goroutine,每个Goroutine都有自己的工作目录,使用os.Chdir函数,而不修改主线程的工作目录。

...
// Goroutine A
go func() {
    os.Chdir("dir_a/")
}()

// Goroutine B
go func() {
    os.Chdir("dir_b/")
}()
...

到目前为止,上述代码无法阻止主线程将其工作目录更改为"dir_b/",而且我确信它还存在Goroutine之间竞争条件的严重风险。

英文:

I was wondering if I could instantiate two different Goroutines, each of them with their own working directory using os.Chdir, without modifying the working directory of the main routine.

...
// Go routine A
go func() {
	os.Chdir("dir_a/")
} ()

// Go routine B
go func() {
	os.Chdir("dir_b/")
} ()
...

So far, the avobe code does not prevent the main thread to change its working directory to "dir_b/", and I am sure it also has a serious risk of a racing condition between each Goroutine.

答案1

得分: 4

可以有两个不同的goroutine拥有不同的工作目录吗?

不可以。你的应用程序只有一个工作目录。

英文:

> Is it possible to have two different goroutines with different working directories?

No. Your application has just one working directory.

huangapple
  • 本文由 发表于 2023年3月22日 20:49:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75812476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定