需要同步Go例程

问题描述 投票:-3回答:2

这个特殊的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。为什么我们要在两个或更多个例程之间引入同步?是回调吗?

谢谢

go goroutine routines
2个回答
4
投票

并发不是在真空中发生的。出于多种原因,可能需要协调您开始使用的goroutine。在这种情况下,有一个主要的(超出示例,设计为演示):

  • 如果达到main方法的末尾,Go运行时将退出。此时,包含所有其他goroutine的运行程序将被终止。这里的协调确保子节点goroutine在允许程序退出之前已经停顿。 我昨天写了一个例子:https://stackoverflow.com/a/52347990/1113760

您可能会想到许多其他原因需要在并发工作之间进行协调(此列表并非特定于Go并且并非详尽无遗):

  • goroutines在共享内存区域上运行,因此需要以互斥的形式进行协调,以确保一次只有一个例程访问临界区。
  • 您的继续处理可能取决于多个goroutine的输出,因此您需要等待达成共识才能继续。
  • 您的程序可能会提供服务,并且您希望在关闭之前确保任何正在进行的请求(在单独的goroutine中处理)已完成并耗尽。

这个问题超越了Go考虑与并发执行相关的计算机科学困难,以及它有用的实例,因此文献将有更多的细节和例子。


0
投票

如果流程可以独立运行,那么事情就非常简单。我们可以在核心中运行每个流程,事情很容易。当进程依赖时(即进程A依赖于进程B),问题就出现了。

实现这一目标的经典方法是共享一个公共内存并将其锁定,以便只有进程可以在给定时间访问它。 Go channel遵循不同的方法,您可以在这里阅读更多相关信息

© www.soinside.com 2019 - 2024. All rights reserved.