为什么在这里发生僵局

zero_coding:

我想了解golang频道的运作方式。我读了一本关于go语言的书,找到了以下示例。

package main

import (
    "fmt"
)

// Send the sequence 2, 3, 4, ... to returned channel 
func generate() chan int {
    ch := make(chan int)
    go func() {
        for i := 2; i <= 100 ; i++ {
            ch <- i
        }
    }()
    return ch
}

// Filter out input values divisible by 'prime', send rest to returned channel
func filter(in chan int, prime int) chan int {
    out := make(chan int)
    go func() {
        for {
            if i := <-in; i%prime != 0 {
                out <- i
            }
        }
    }()
    return out
}

func sieve() chan int {
    out := make(chan int)
    go func() {
        ch := generate()
        for {
            prime := <-ch
            ch = filter(ch, prime)
            out <- prime
        }
    }()
    return out
}

func main() {
    primes := sieve()
    for {
        fmt.Println(<-primes)
    }
}

当我运行该程序时,出现了死锁,但是当我将generate函数更改为

// Send the sequence 2, 3, 4, ... to returned channel 
func generate() chan int {
    ch := make(chan int)
    go func() {
        for i := 2; ; i++ {
            ch <- i
        }
    }()
    return ch
}

然后,程序将运行无限循环,但不会死锁。当我删除for循环中的条件时,为什么会出现死锁?

VonC:

阻塞原理是什么意思?

你可以看到它在博客文章“图文并茂的自然通道走

对于无缓冲通道:

http://3.bp.blogspot.com/-vnJIWvlbP-E/UwDVICJKB9I/AAAAAAAANX0/T04V_58i8Vs/s1600/Screen+Shot+2014-02-16+at+10.10.54+AM.png

(摘自2014年2月William Kennedy撰写的博客文章“ The Channels in Go

Unbuffered channels have no capacity and therefore require both goroutines to be ready to make any exchange.
When a goroutine attempts to write a resource to an unbuffered channel and there is no goroutine waiting to receive the resource, the channel will lock the goroutine and make it wait.
When a goroutine attempts to read from an unbuffered channel, and there is no goroutine waiting to send a resource, the channel will lock the goroutine and make it wait.

That is what happens in your case with your reader:

func main() {
    primes := sieve()
    for {
        fmt.Println(<-primes)
    }
}

since primes is never closed, main remains blocked.
It (main) is in step 3:

在第3步中,右侧的goroutine将手放到通道中或执行read
该goroutine也锁定在通道中,直到交换完成

发件人从不打电话close(primes)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章