我有两个结构体App和Config。
type App struct {
cfg Config
}
type Config struct {
dbHost string
dbPort int
user string
password string
}
以及App上定义的两个方法来更新和读取cfg字段。
func (app *App) UpdateConfig(newCfg Config) {
app.cfg = newCfg
}
func (app *App) GetConfig() Config {
return app.cfg
}
如果只有一个 goroutine 正在调用UpdateConfig,并且多个 goroutine 正在通过方法读取配置GetConfig,我是否应该使用app.cfg互斥锁来保护对访问的访问?
GetConfig编辑:Reader goroutine在 for 循环中调用。不需要“立即”查看配置的更新值。读者可以在下一次迭代中看到cfg的更新值。
所以我重新表述我的问题:读者是否可以看到部分更新的配置值?
蓝山帝景
相关分类