dqm4675 2018-03-08 13:08
浏览 40
已采纳

如何解决此程序中的死锁?

I am new to golang, so this deadlock issue is unknown for me. I've been Reading some articles, but it seems to be a quick fix in this program.

package main

import (
    "fmt"
)

//ping
func addValue(input1 int, input2 int, chn2 chan<- int) {
    chn2 <- input1 + input2
}

//pong
//function to write 1st user input to channel_1
func getUserInput(input1 int, input2 int, chn2 <-chan int, chn1 chan int, chn3 chan int) {
    chn1 <- input1
    chn1 <- input2
    //receiving info from other func and read into this func.
    val := <-chn2
    chn3 <- val
}

//Main function
func main() {
    var input1 int
    var input2 int
    chn1 := make(chan int, 3)
    chn2 := make(chan int)
    chn3 := make(chan int)

    //taking inputs from terminal
    fmt.Scanln(&input1)
    fmt.Scan(&input2)

    //calling go functions
    go getUserInput(input1, input2, chn1, chn2, chn3)
    go addValue(input1, input2, chn2)

    //shifting values from channels to var.
    /*x:=
      y :=
      z := <-chn3*/

    //print out the values on the terminal
    fmt.Println("Reading first input: ", <-chn1)
    fmt.Println("Reading second input: ", <-chn1)
    fmt.Println("Giving resulted value: ", <-chn3)

}
  • 写回答

2条回答 默认 最新

  • doulan7166 2018-03-08 13:26
    关注

    You had function argument order wrong. Just change the function header and you are good

    Change

    func getUserInput(input1 int, input2 int, chn2 <-chan int, chn1 chan int, chn3 chan int) {
    

    to

    func getUserInput(input1 int, input2 int, chn1 chan int,chn2 <-chan int,  chn3 chan int) {
    

    Play link :https://play.golang.com/p/3lKLoEytr9J

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办