go - 我什么时候应该把对象放回 sync.Pool

标签 go memory-management channel pool goroutine

我在 goroutines 之间通过 channel 传递一个大对象,一个 goroutine 用于发布,许多 goroutines 订阅和等待消息,它是如此频繁,我想使用 sync.Pool 来减少数量分配,示例代码如下:

package main

import (
    "log"
    "sync"
    "time"
)

var pool *sync.Pool

type object struct {
    info string
    // other stuff
}

func initPool() {
    pool = &sync.Pool{
        New: func() interface{} {
            return new(object)
        },
    }
}

var (
    lock     sync.RWMutex
    registry = make(map[string][]chan interface{})
)

func sub(topic string) chan interface{} {
    ch := make(chan interface{}, 10)
    lock.Lock()
    registry[topic] = append(registry[topic], ch)
    lock.Unlock()
    return ch
}

// publish goroutine
func pub() {
    ticker := time.NewTicker(time.Second)
    o := pool.Get().(*object)
    o.info = "new"
    // do something

    for _ = range ticker.C {
        lock.RLock()
        for topic, chs := range registry {
            if o.info != "new" {
                log.Printf("error")
            }
            if topic == "hello" {
                for _, ch := range chs {
                    select {
                    case ch <- o:
                    default:
                    }
                }
            }
        }
        lock.RUnlock()
    }
}

func run(topic string) {
    ch := sub(topic)
    for {
        select {
        case o := <-ch:
            switch o := o.(type) {
            case *object:
                if o.info != "new" {
                    log.Printf("error")
                }

                // do something
                time.Sleep(time.Second)
                o.info = "used"
                pool.Put(o)
            }
        }
    }
}

func main() {
    initPool()

    for i := 0; i <= 100; i++ {
        go run("hello")
    }
    pub()
}

问题是从 pool 借来的对象可以被多个 goroutine 访问,所以如果一个 goroutine 使用后放回 pool,可能会被其他 goroutine 修改。我不知道什么时候所有的 goroutines 都完成了这个对象,所以我可以把它放回池中。

最佳答案

应该在处理完池对象后调用pool.Put(),即它被认为是“垃圾”。

如果您将 o 放回 pub 中的池中,那么循环的下一次迭代可以获得相同的对象并为其之前的属性分配新值它已被 run 处理。因此,在处理完对象后,将对象放回池中的正确位置是 run

关于go - 我什么时候应该把对象放回 sync.Pool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51914995/

相关文章:

loops - Go 模板中的嵌套循环

go - 重新打包 C 类型导致 Go 中出现类型转换错误

time - 获取 time.Time 月份的最后一天

c++ - 有人可以解释为什么 int 会根据系统架构呈现不同的大小吗?

戈朗 : strange issue with coroutines and channels

go - HTTP 客户端,空闲超时

linux - Linux 中的空闲内存?

function - 如何在 Mathematica 和 Matlab 中查找已定义符号的类型和内存位置?

algorithm - 可靠的组播算法没有可靠的单播?

go channel capacity 为什么比我指定的多了一个元素