简体   繁体   English

如何有效地将goroutine中分配的结构通过通道传递回主例程?

[英]How do I efficiently pass a struct allocated in a goroutine back to the main routine via a channel?

At a basic level, I have a main routine that spawns multiple goroutines to process data. 在基本级别上,我有一个主例程,该例程生成多个goroutine来处理数据。 Every time a goroutine processes the data it sends back a struct of varying size (it contains slices and/or arrays allocated from within the goroutine each time). 每次goroutine处理数据时,它都会发回一个大小可变的结构(每次都包含从goroutine内部分配的切片和/或数组)。

The data isn't huge (say, a few megabytes) but in general is it more efficient (and is it safe) to transfer a pointer to the data versus a copy of it all? 数据不是很大(例如,几兆字节),但是总的来说,将指针转移到数据而不是全部副本是否更有效(并且更安全)? If the data structure is static and I transfer a pointer to it, there's a risk that the structure may change while I'm still processing the result of the previous invocation (if it's fully reallocated then perhaps that's not an issue). 如果数据结构是静态的,并且我将指针传递给它,则有可能在我仍在处理上一次调用的结果时更改结构(如果它已完全重新分配,那么这可能不是问题)。

It's OK and common to send pointers to values. 发送指向值的指针是可以的并且很常见。 If the value is large, sending a pointer to the value will be more efficient than sending the value. 如果该值较大,则发送指向该值的指针将比发送该值更有效。 Run a benchmark to find out how large is "large". 运行基准以找出“大”的大小。

The caveat is that you must prevent unsafe concurrent access to the value. 注意,您必须防止不安全的并发访问该值。 Common strategies for preventing unsafe concurrent access are: 防止不安全的并发访问的常见策略是:

  • Pass ownership of the value from the sender to the receiver. 将值的所有权从发送方传递到接收方。 The sender does not access the value after sending it. 发送者在发送后将不访问该值。 The receiver can do whatever it wants with the value. 接收者可以用该值做任何想要的事情。
  • Treat the value as read only after sending. 发送后将值视为只读。 Neither the sender or receiver modifies the value after sending. 发送方或接收方在发送后都不会修改该值。

From my understanding you're trying to do something like: 据我了解,您正在尝试执行以下操作:

func watchHowISoar() (ch chan *bigData) {
    ch = make(chan *bigData)
    go func() {
        for i := 0; i < 10; i++ {
            bd := &bigData{i}
            ch <- bd
            // as long as you don't modify bd inside this goroutine after sending it, you're safe.
        }
        close(ch)
    }()
    return
}
func main() {
    for iamaleafOnTheWind := range watchHowISoar() {
        fmt.Printf("%p\n", iamaleafOnTheWind)
    }
}

And it is perfectly safe as long as you don't modify the sent data from the sender after you send it. 只要您在发送后不修改发件人发送的数据,这是绝对安全的。

If you have doubts, try to run it with go run -race main.go , while the race detector isn't perfect, it will usually detect things like that. 如果您有疑问,请尝试使用go run -race main.go来运行它,而种族检测器并不完美,它通常会检测出类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将头从main传递到addNode函数? (通过struct实现LinkedList) - How to pass head from main to addNode function ? (LinkedList implementation via struct) 如何将排序后的结构返回到主函数? - How do I return a sorted struct to my main function? 我该怎么做呢? 获取结构以接口的形式发送到func并将其作为结构返回? - How do I do this? Get an struct send to a func as interface and get it back as struct? C:结构少了一个字段。 如何有效声明/使用它? - C: struct with one less field. How do I declare/use it efficiently? 如何在MATLAB中将结构字段作为函数输出传递? - How do I pass a struct field as a function output in MATLAB? 如何在 c 的 pthread 中将结构作为参数传递 - How do i pass a struct as an argument in pthread in c 如何检索通过调用C#返回的Struct - How do I retrieve a Struct returned via an Invoke C# 如何通过引用将存储在列表中的结构成员传递给函数? - How do I pass struct members stored in a list to a function by reference? 如何在Objective-C中传递(按值)结构? - How do I pass (by value) a struct in Objective-C? 如何通过引用传递创建到结构的指针数组? - How do I pass by reference an array of pointers created into a struct?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM