Go语言 何时使用终结器关闭通道?

lzfw57am  于 12个月前  发布在  Go
关注(0)|答案(1)|浏览(83)

这是两个问题中的第二个(这是first one),以帮助理解Go泛型提案examples
特别是,到目前为止,我很难理解标题为“Channels”的提案的示例部分中的两段代码:
第二个问题是在下面的Ranger函数的定义中。
也就是说,我不明白为什么需要调用runtime.SetFinalizer(r,r.finalize),而实际上*Receiver[T]类型的finalize)方法应该做的只是发出信号,表明接收器已经完成接收值(close(r.done))。
在我看来,通过为*Receiver[T]提供终结器,代码将关闭接收器的义务委托给了运行时。
我理解这段代码的方式是,*Receiver[T]*Sender[T]发出信号,当GC决定前者不可访问时,它将不再接收任何值,即没有更多的引用可用。
如果我的解释是正确的,为什么要等那么长时间才让接收器发出信号呢?是否可以在代码中以某种方式显式处理close操作?
代码:

// Ranger provides a convenient way to exit a goroutine sending values
// when the receiver stops reading them.
//
// Ranger returns a Sender and a Receiver. The Receiver provides a
// Next method to retrieve values. The Sender provides a Send method
// to send values and a Close method to stop sending values. The Next
// method indicates when the Sender has been closed, and the Send
// method indicates when the Receiver has been freed.
func Ranger[T any]() (*Sender[T], *Receiver[T]) {
    c := make(chan T)
    d := make(chan bool)
    s := &Sender[T]{values: c, done: d}
    r := &Receiver[T]{values: c, done: d}
    // The finalizer on the receiver will tell the sender
    // if the receiver stops listening.
    runtime.SetFinalizer(r, r.finalize)
    return s, r
}

// A Sender is used to send values to a Receiver.
type Sender[T any] struct {
    values chan<- T
    done   <-chan bool
}

// Send sends a value to the receiver. It reports whether any more
// values may be sent; if it returns false the value was not sent.
func (s *Sender[T]) Send(v T) bool {
    select {
    case s.values <- v:
        return true
    case <-s.done:
        // The receiver has stopped listening.
        return false
    }
}

// Close tells the receiver that no more values will arrive.
// After Close is called, the Sender may no longer be used.
func (s *Sender[T]) Close() {
    close(s.values)
}

// A Receiver receives values from a Sender.
type Receiver[T any] struct {
    values <-chan T
    done  chan<- bool
}

// Next returns the next value from the channel. The bool result
// reports whether the value is valid. If the value is not valid, the
// Sender has been closed and no more values will be received.
func (r *Receiver[T]) Next() (T, bool) {
    v, ok := <-r.values
    return v, ok
}

// finalize is a finalizer for the receiver.
// It tells the sender that the receiver has stopped listening.
func (r *Receiver[T]) finalize() {
    close(r.done)
}
nbysray5

nbysray51#

**TLDR:**您的理解是正确的,done通道可能只是由接收方“手动”关闭,以表示失去兴趣(停止通信并解除发送方的职责)。

通道用于goroutine以并发安全的方式进行通信。习惯用法是发送方不断发送值,一旦没有更多的值要发送,发送方就会关闭通道。
接收方一直从通道接收,直到通道关闭,这表明通道上不会(不可能)有任何更多的值。这通常/最容易通过通道使用for range完成。
所以 * 通常 * 接收方必须继续接收,直到通道关闭,否则发送方将永远被阻止。通常这是OK /足够的。
所演示的Ranger()构造用于非一般情况,即接收方需要/可能停止通信。
单个信道不提供用于接收方向发送方发信号通知接收方已经失去兴趣的手段,并且不需要更多的值。这需要接收方必须关闭的附加信道(当然,发送方必须监视)。只要有一个接收器,这也是可以的。但是如果有多个接收器,关闭done通道会变得有点复杂:所有接收器都不能关闭done通道:关闭已经关闭的通道会发生恐慌。因此,接收器也必须被协调,因此只有单个接收器,或者更确切地说,协调器方本身仅关闭done信道一次;并且这必须在所有接收机“放弃”信道之后发生。
Ranger()对此有所帮助,并且通过使用终结器委托关闭done通道的简单方式。这是可以接受的,因为通常情况下,停止通信甚至不是接收方的任务,但在极少数情况下,如果仍然出现这种情况,它将被处理(以一种简单的方式,不需要额外的协调器goroutine)。

相关问题