- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有以下 Go 代码
package main
import (
"fmt"
"math/rand"
)
const (
ROCK int = iota
PAPER
SCISSORS
)
type Choice struct {
Who int //0 you 1 your opponent
Guess int
}
//Win returns true if you win.
func Win(you, he int) bool {
...
}
func Opponent(guess chan Choice, please chan struct{}) {
for i := 0; i < 3; i++ {
<-please
choice := rand.Intn(3)
who := 1
guess <- Choice{who, choice}
please <- struct{}{}
}
}
func GetChoice(you, he int) int {
...
}
var Cheat func(guess chan Choice) chan Choice = func(guess chan Choice) chan Choice {
new_guess := make(chan Choice)
// go func() {
for i := 0; i < 3; i++ {
g1 := <-guess
g2 := <-guess
if g1.Who == 0 {
choice := GetChoice(g1.Guess, g2.Guess)
new_guess <- g2
new_guess <- Choice{g1.Who, choice}
} else {
choice := GetChoice(g2.Guess, g1.Guess)
new_guess <- g1
new_guess <- Choice{g2.Who, choice}
}
}
// }()
fmt.Println("...")
return new_guess
}
func Me(guess chan Choice, please chan struct{}) {
for i := 0; i < 3; i++ {
<-please
choice := rand.Intn(3)
who := 0
guess <- Choice{who, choice}
please <- struct{}{}
}
}
func Game() []bool {
guess := make(chan Choice)
//please sync 2 goroutines.
please := make(chan struct{})
go func() { please <- struct{}{} }()
go Opponent(guess, please)
go Me(guess, please)
guess = Cheat(guess)
var wins []bool
for i := 0; i < 3; i++ {
g1 := <-guess
g2 := <-guess
win := false
if g1.Who == 0 {
win = Win(g1.Guess, g2.Guess)
} else {
win = Win(g2.Guess, g1.Guess)
}
wins = append(wins, win)
}
return wins
}
func main() {
win := Game()
fmt.Println(win)
}
当我运行这段代码时,我收到错误fatal error: all goroutines are asleep - deadlock!。但是,当我取消注释 Cheat 函数中的 go func() 行时,错误消失了。我无法理解为什么错误出现在第一种情况下以及为什么在使用 goroutine 时错误消失了。那么,如果有人可以解释一下?
最佳答案
在这个简化的例子中:
func Cheat(guess chan Choice) chan Choice {
new_guess := make(chan Choice)
new_guess <- Choice{}
<-guess
return new_guess
}
当写入新分配的 channel 时,没有其他人可能拥有该 channel ,因此写入将永远阻塞。由于写入 block ,从 guess
读取永远不会发生。但是,在您引用的代码中,Cheat()
函数是唯一从guess
channel 读取的函数;所以写入它的东西在读取发生时被阻止,读取不会发生,直到写入 new_guess
发生,并且写入不会发生,直到包含函数返回。
如果将 channel I/O 移动到 goroutine 中,则包含函数可以在事情进展之前返回,因此 Cheat()
中的写入与 末尾的读取配对code>Game()
事情可以继续进行。
关于go - fatal error : all goroutines are asleep - deadlock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51257837/
发件人:http://blog.nindalf.com/how-goroutines-work/ As the goroutines are scheduled cooperatively, a go
很多时候在用 Go 开发 http 服务器时,我都会遇到这种困境。 假设我想尽快用http statuscode 200响应客户端(然后在后面执行工作),这就是我通常这样做的原因: 我让我的主要 ht
这是代码: import "fmt" func main() { messages := make(chan string, 1) go func(c chan string) {
我正在学习 Golang,但遇到了一些困难。我已经研究过 Google,但没有任何进展。 我编写了一个代码,通过多台服务器的 ICMP 检查 RTT。 它有这样的结构: type Server str
我想运行多个 goroutine,进行一些处理,将结果放入 channel ,当至少有一个 goroutine 完成时,完成所有其他 goroutine 并从 channel 返回结果。 所以,我尝试
我有两个(但以后我会是三个)go 例程来处理来自远程服务器(来自 ampq channel )的传入消息。但是因为它们正在处理相同的数据/状态,所以我想阻止所有其他 go 例程,除了正在运行的例程。
我有一个案例,我从 2 个不同的位置(ES 和 REDIS)读取数据,我需要从最快的源读取一个值,因此我触发了 2 个 goroutines,一个从 ES 获取数据,其他从REDIS获取。 一旦从其中
像这里一样,我创建了一个 go playground 示例:sGgxEh40ev ,但无法正常工作。 quit := make(chan bool) res := make(chan int) go
我是golang的新手,正在研究goroutine。 我写了一个简单的代码,故意使用 goroutine 来划分数字。 首先,我给出基数并继续除它的数,直到它不能被整除 但是,我改变了go split
Main { go routine_1(carryout a time consuming task and return output) go routine_2(wait for output f
我想知道从另一个 goroutine 返回时调用的 goroutine 会发生什么。他们是继续运行还是被终止?这是一个示例代码来说明我的意思: func func() { // Doing s
更具体地说,在我的例子中,我有一个网络服务器和一个全局可访问的结构,网络服务器使用它来生成页面。我有另一个 Goroutine,它总是定期用新值更新该结构。这会引起问题吗?我是否需要实现一种机制来确保
来自 this file ,我不明白为什么函数startWorker会这样写: func (p *WorkerPool) dispatch() { for i := 0; i < p.maxW
我正在学习围棋,但在使用 goroutines 时遇到了问题。这是我的代码 package main import ( "fmt" "sync" "time" ) var co
我收到以下错误,我不明白为什么: 发送:查询 Herefatal 错误:所有 goroutines 都睡着了 - 死锁! 您可以看到我正在调用我使用 goroutine 创建的函数 routine。我
大家好,我正在从 Python3 过渡到 Go,所以我正在尝试重写我创建的库以获得更好的性能。 我面临一个问题,因为我是 Golang XD 中的新手,我使用有限的 API 下载数百个 json,我想
我有以下格式的脚本部分: func main() { for i=0;i<1000000;i++ { go test() } } func test() { a := test
package main func main() { c:=make(chan int) for i:=0; i<=100;i++ {
我正在学习 Go,我的第一个项目是一个简单的 ping 脚本。本质上,我想 ping 一堆 url,并在每个响应时等待 XXX 秒,然后再次 ping。这是删减的代码: func mai
这个问题在这里已经有了答案: Go all goroutines are asleep deadlock (2 个回答) fatal error: all goroutines are asleep
我是一名优秀的程序员,十分优秀!