登录
首页 >  Golang >  Go问答

何时应考虑使用终结器来关闭通道?

来源:stackoverflow

时间:2024-03-20 23:18:44 338浏览 收藏

在 Go 泛型提案中,“何时应考虑使用终结器来关闭通道?”是一个常见问题。Ranger 函数通过使用终结器来解决接收方需要/可能停止通信的非一般情况。传统的通道无法让接收方向发送方发出信号,表明接收方已失去兴趣。Ranger 函数通过使用额外的通道和终结器来实现此功能,当接收方停止接收时,终结器将关闭该通道,向发送方发出信号。

问题内容

这是两个问题中的第二个问题(这是第一个问题),旨在帮助理解 go 泛型提案示例。

特别是,到目前为止,我在理解标题为“通道”的提案示例部分中的两段代码时遇到了麻烦:

我遇到的第二个问题是 ranger 函数的以下定义。

也就是说,我不明白需要调用 runtime.setfinalizer(r,r.finalize) 实际上 *receiver[t] 类型的 finalize) 方法应该做的只是表示接收器已完成接收值的信号 (close(r.done))。

在我看来,通过为 *receiver[t] 提供终结器,代码将关闭接收器的义务委托给运行时。

我理解这段代码的方式是,当 gc 决定前者是时, *receiver[t]*sender[t] 发出信号,它将不再接收任何值。无法访问,即没有更多可用的引用。

如果我的解释是正确的,为什么要等那么久接收方才发出信号已完成?难道不能以某种方式在代码中显式处理 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)
}

正确答案


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

通道用于让 goroutine 以并发安全的方式进行通信。惯用的用法是发送方不断发送值,一旦没有更多的值可发送,发送方就会发出关闭通道的信号。

接收方不断从通道接收数据,直到通道关闭,这表明通道上不会(不能)再有任何值。这通常/最容易通过通道使用 for range 来完成。

所以通常接收方必须继续接收直到通道关闭,否则发送方将永远被阻止。通常这是可以/足够的。

演示的 Ranger() 构造适用于接收方需要/可能停止通信的非一般情况。

单个通道无法让接收方向发送方发出信号,表明接收方已失去兴趣,并且不需要更多值。这需要一个额外的通道,接收者必须关闭该通道(当然发送者也必须监视)。只要有一个接收器,这也可以。但如果有多个接收者,关闭 done 通道会变得有点复杂:所有接收者都不能关闭 done 通道:关闭已经关闭的通道会出现恐慌。因此接收方也必须进行协调,因此只有一个接收方,或者更确切地说,协调方本身关闭 done 通道,仅一次;这必须在所有接收者“放弃”频道之后发生。

Ranger() 通过使用终结器委托关闭 done 通道来帮助解决此问题。这是可以接受的,因为通常停止通信甚至不是接收者的任务,但在极少数情况下,如果这种情况仍然出现,它将被处理(以一种简单的方式,不需要额外的,协调器 goroutine)。

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>