confused about the depth field assignment way when retrieveMetaData()
carbin-gun opened this issue · comments
// retrieveMetaData initializes state from the filesystem
func (d *diskQueue) retrieveMetaData() error {
var f *os.File
var err error
fileName := d.metaDataFileName()
f, err = os.OpenFile(fileName, os.O_RDONLY, 0600)
if err != nil {
return err
}
defer f.Close()
var depth int64
_, err = fmt.Fscanf(f, "%d\n%d,%d\n%d,%d\n",
&depth,
&d.readFileNum, &d.readPos,
&d.writeFileNum, &d.writePos)
if err != nil {
return err
}
atomic.StoreInt64(&d.depth, depth)
d.nextReadFileNum = d.readFileNum
d.nextReadPos = d.readPos
return nil
}
I noticed that the depth was assigned to a local depth variable. then use the atomic.StoreInt64
to set the local depth value to the struct field. I'm confused for this, Is there anything special for the d.depth
, why not just
_, err = fmt.Fscanf(f, "%d\n%d,%d\n%d,%d\n",
&d.depth,
&d.readFileNum, &d.readPos,
&d.writeFileNum, &d.writePos)
It's because elsewhere we read just the depth value atomically.