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

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格