On Tue, Sep 14, 2021 at 9:30 AM LinkinStar <linkins...@foxmail.com> wrote: > > Hi golang-nuts, > When I read the source code about chan.go, I found some problems that were a > little difficult to understand. > > hchan has two function, full and empty. > > func full(c *hchan) bool { > // c.dataqsiz is immutable (never written after the channel is created) > // so it is safe to read at any time during channel operation. > if c.dataqsiz == 0 { > // Assumes that a pointer read is relaxed-atomic. > return c.recvq.first == nil > } > // Assumes that a uint read is relaxed-atomic. > return c.qcount == c.dataqsiz > } > > > func empty(c *hchan) bool { > // c.dataqsiz is immutable. > if c.dataqsiz == 0 { > return atomic.Loadp(unsafe.Pointer(&c.sendq.first)) == nil > } > return atomic.Loaduint(&c.qcount) == 0 > } > > > why does the empty function use atomic while the full function does not?
See the lengthy comment above the only call of full, which explains why it's OK to reorder the memory loads. Ian -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcWreHcWMxesuvty5PcbV7hDmfFKNH1nmBQ%2BWL_zqeN%3Dwg%40mail.gmail.com.