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条)

报告相同问题?

悬赏问题

  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?
  • ¥15 Ubuntu20.04.4.LTS系统如何下载安装VirtualBox虚拟机?