英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论