英文:
How can I fix the deadlock?
问题
程序必须在我写入第一个通道时打印num的平方根,如果写入第二个通道,则打印3*num,如果写入stop通道,则结束程序。我无法理解通道被阻塞的位置。
package main
import (
"fmt"
)
func main() {
ch1, ch2 := make(chan int), make(chan int)
stop := make(chan struct{})
r := calculator(ch1, ch2, stop)
//ch1 <- 3
ch2 <- 2
//<-stop
fmt.Println(<-r)
}
func calculator(firstChan <-chan int, secondChan <-chan int, stopChan <-chan struct{}) <-chan int {
returnChan := make(chan int)
go func() {
defer close(returnChan)
select {
case <-firstChan:
returnChan <- (<-firstChan) * (<-firstChan)
case <-secondChan:
returnChan <- (<-secondChan) * 3
case <-stopChan:
close(returnChan)
}
}()
return returnChan
}
英文:
The programm must print sqrt of num if I write to first channel, 3*num if to secon, complete if to stop. I can't understand where is channel blocked.
package main
import (
"fmt"
)
func main() {
ch1, ch2 := make(chan int), make(chan int)
stop := make(chan struct{})
r := calculator(ch1, ch2, stop)
//ch1 <- 3
ch2 <- 2
//<-stop
fmt.Println(<-r)
}
func calculator(firstChan <-chan int, secondChan <-chan int, stopChan <-chan struct{}) <-chan int {
returnChan := make(chan int)
go func() {
defer close(returnChan)
select {
case <-firstChan:
returnChan <- (<-firstChan) * (<-firstChan)
case <-secondChan:
returnChan <- (<-secondChan) * 3
case <-stopChan:
close(returnChan)
}
}()
return returnChan
}
答案1
得分: 1
你正在阅读的是 case 语句:
case <-firstChan
在 case 块内部是:
...(<-firstChan) * (<-firstChan)
当只有一个值发送到通道中时,你总共读取了三次。
在 case 语句中将值存储到一个变量中,并在块中使用它,如下所示:
func calculator(firstChan <-chan int, secondChan <-chan int, stopChan <-chan struct{}) <-chan int {
returnChan := make(chan int)
go func() {
defer close(returnChan)
select {
case firstChanVal := <-firstChan:
returnChan <- firstChanVal * firstChanVal
case firstChanVal := <-secondChan:
returnChan <- firstChanVal * 3
case <-stopChan:
close(returnChan)
}
}()
return returnChan
}
英文:
You are reading from the case statement:
case <-firstChan
and within case block:
...(<-firstChan) * (<-firstChan)
all together you read three times when only one was sent into the channel.
Get the value to a var in the case statement and use it in the block like below:
func calculator(firstChan <-chan int, secondChan <-chan int, stopChan <-chan struct{}) <-chan int {
returnChan := make(chan int)
go func() {
defer close(returnChan)
select {
case firstChanVal := <-firstChan:
returnChan <- firstChanVal * firstChanVal
case firstChanVal := <-secondChan:
returnChan <- firstChanVal * 3
case <-stopChan:
close(returnChan)
}
}()
return returnChan
}
答案2
得分: 0
当你调用<-firstChan时,程序将暂停并等待一个值发送到firstChan,你调用了3次<-firstChan,但只发送了1次,程序将永远暂停。
英文:
When you call <-firstChan, the program will be paused and wait for a value to send to firstChan, you call 3 times <-firstChan while you only send 1 time, the program will pause forever
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论