dstbp22002 2018-03-29 23:27
浏览 48
已采纳

Go程序不会死锁,但也永远不会返回

The attached gist is a simple program using channels in a producer / multi-consumer model. For some reason,

go run channels.go prints all the results but does not return (and does not deadlock or at least go doesn't give me that panic that a deadlock occurs.)

type walkietalkie struct {
    in chan int
    out chan int
    quit chan bool
}

var items []int = []int{
    0, 1, 2, 3, 4, 5,
}

func work1(q walkietalkie) {
    for {
        select {
        case a, more := <- q.in:
            if more {
                q.out <- a * 2
            }
        default:
            break
        }
    }
}

func work2(q walkietalkie) {
    for  {
        select {
        case a, more := <- q.in:
            if more {
                q.out <- a * -1
            }
        default:
            break
        }
    }
}

func work3(q walkietalkie) {
    for  {
        select {
        case a, more := <- q.in:
            if more {
                q.out <- a * 7
            }
            default:
                break
        }
    }
}

func main() {
    results := make(chan int, 18)
    defer close(results)

    w := []walkietalkie{
        walkietalkie{ in: make(chan int, 6), out: results, quit: make(chan bool, 1) },
        walkietalkie{ in: make(chan int, 6), out: results, quit: make(chan bool, 1) },
        walkietalkie{ in: make(chan int, 6), out: results, quit: make(chan bool, 1) },
    }

    go work1(w[0])
    go work2(w[1])
    go work3(w[2])

    // Iterate over work items
    l := len(items)
    for i, e := range items {
        // Send the work item to each worker
        for _, f := range w {
            f.in <- e // send the work item
            if i == l - 1 { // This is the last input, close the channel
                close(f.in)
            }
        }
    }

    // Read all the results from the workers
    for {
        select {
        case r, more := <-results:
            if more {
                fmt.Println(r)
            } else {
                continue
            }
        default:
            break
        }
    }

}
  • 写回答

1条回答 默认 最新

  • doucheng7234 2018-03-29 23:51
    关注

    You have a few problems.

    For 1, reading from a channel with multiple return values like

    case a, more := <-q.in 
    

    Will proceed on a closed channel, with more being set to false. In your case the default is never hit.

    But those are in goroutines and wouldn't stop the program from exiting. The problem is your main goroutine is doing the same thing. Also, as it turns out, break will break out of selects as well as for loops. So if you want to break the for loop then you need to use a label and break LABEL.

    As an alternative, you could also just return instead of breaking in your main goroutine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题