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

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

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

问题

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

  1. ...
  2. // Goroutine A
  3. go func() {
  4. os.Chdir("dir_a/")
  5. }()
  6. // Goroutine B
  7. go func() {
  8. os.Chdir("dir_b/")
  9. }()
  10. ...

到目前为止,上述代码无法阻止主线程将其工作目录更改为"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.

  1. ...
  2. // Go routine A
  3. go func() {
  4. os.Chdir("dir_a/")
  5. } ()
  6. // Go routine B
  7. go func() {
  8. os.Chdir("dir_b/")
  9. } ()
  10. ...

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:

确定