I have sharable object. Let's call it OBJ.
I wanted rarely update it. And to be able to read it frequently without locking.
I've tried this:
func Initialize() (func() func() OBJ, func()) {
genFunc := func() func() OBJ {
OBJ := getNewObject()
secretFunc := func() OBJ {
return OBJ
}
return secretFunc
}
secretFunc := genFunc()
timer := time.NewTicker(time.Duration(timeoutSec) * time.Second)
finish := make(chan bool)
go func() {
for {
select {
case <-finish:
close(finish)
return
case <-timer.C:
secretFunc = genFunc()
}
}
}()
retFunc := func() func() OBJ {
return secretFunc
}
shutDownRet := func() {
finish <- true
timer.Stop()
}
return retFunc, shutDownRet
}
In this example, real sharable is not an OBJ, but function "secretFunc".
Is it thread/goroutine safe to do this trick with replacement of secretFunc without any locking? I would like to avoid using CAS
& unsafe.Pointers
.
If this is not safe, what is the pattern (if it exists) to implement this to be safe?