dqcd84732 2017-07-24 18:14
浏览 42
已采纳

golang线程模型比较

I have a piece of data

type data struct {
    // all good data here
    ...
}

This data is owned by a manager and used by other threads for reading only. The manager needs to periodically update the data. How do I design the threading model for this? I can think of two options:

1.

type manager struct {
    // acquire read lock when other threads read the data.
    // acquire write lock when manager wants to update.
    lock sync.RWMutex
    // a pointer holding a pointer to the data
    p *data
}

2.

type manager struct {
    // copy the pointer when other threads want to use the data.
    // When manager updates, just change p to point to the new data.
    p *data
}

Does the second approach work? It seems I don't need any lock. If other threads get a pointer pointing to the old data, it would be fine if manager updates the original pointer. As GoLang will do GC, after all other threads read the old data it will be auto released. Am I correct?

  • 写回答

1条回答 默认 最新

  • dt3358 2017-07-24 19:35
    关注

    Your first option is fine and perhaps simplest to do. However, it could lead to poor performance with many readers as it could struggle to obtain a write lock.

    As the comments on your question have stated, your second option (as-is) can cause a race condition and lead to unpredictable behaviour.

    You could implement your second option by using atomic.Value. This would allow you to store the pointer to some data struct and atomically update this for the next readers to use. For example:

    // Data shared with readers
    type data struct {
       // all the fields
    }
    
    // Manager
    type manager struct {
        v atomic.Value
    }
    
    // Method used by readers to obtain a fresh copy of data to 
    // work with, e.g. inside loop
    func (m *manager) Data() *data {
        return m.v.Load().(*data)
    }
    
    // Internal method called to set new data for readers
    func (m *manager) update() {
        d:=&data{
            // ... set values here
        }
        m.v.Store(d)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ADS生成的微带线为什么是蓝色空心的
  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow
  • ¥15 densenet网络结构中,特征以cat方式复用后是怎么进行误差回传的
  • ¥15 STM32G471芯片spi设置了8位,总是发送16位
  • ¥15 R语言并行计算beta-NTI中tree文件的类型
  • ¥15 如何解读marsbar导出的ROI数据?
  • ¥20 求友友协助弄一下基于STC89C52单片机的声光控制灯原理图
  • ¥15 arduino双向交通灯设计
  • ¥15 有没有会粒子群算法的大能(○゜ε^○)求带不会出收敛图😭