这个特殊的Go代码使用一个通道来同步goroutines
。
// We can use channels to synchronize execution
// across goroutines. Here's an example of using a
// blocking receive to wait for a goroutine to finish.
package main
import "fmt"
import "time"
// This is the function we'll run in a goroutine. The
// `done` channel will be used to notify another
// goroutine that this function's work is done.
func worker(done chan bool) {
fmt.Print("working...")
time.Sleep(time.Second)
fmt.Println("done")
// Send a value to notify that we're done.
done <- true
}
func main() {
// Start a worker goroutine, giving it the channel to
// notify on.
done := make(chan bool, 1)
go worker(done)
// Block until we receive a notification from the
// worker on the channel.
<-done
}
有什么需要同步goroutines?我们不会以交错的方式运行goroutines。为什么我们要在两个或更多个例程之间引入同步?是回调吗?
谢谢
并发不是在真空中发生的。出于多种原因,可能需要协调您开始使用的goroutine。在这种情况下,有一个主要的(超出示例,设计为演示):
main
方法的末尾,Go运行时将退出。此时,包含所有其他goroutine的运行程序将被终止。这里的协调确保子节点goroutine在允许程序退出之前已经停顿。
我昨天写了一个例子:https://stackoverflow.com/a/52347990/1113760。您可能会想到许多其他原因需要在并发工作之间进行协调(此列表并非特定于Go并且并非详尽无遗):
这个问题超越了Go考虑与并发执行相关的计算机科学困难,以及它有用的实例,因此文献将有更多的细节和例子。
如果流程可以独立运行,那么事情就非常简单。我们可以在核心中运行每个流程,事情很容易。当进程依赖时(即进程A依赖于进程B),问题就出现了。
实现这一目标的经典方法是共享一个公共内存并将其锁定,以便只有进程可以在给定时间访问它。 Go channel遵循不同的方法,您可以在这里阅读更多相关信息