Summary after reading draveness’s introduction of Chan


readDraveness introduction to ChanSummary and questions after

  1. Chan realizes the effect of sharing memory through communication proposed by go

  2. Chan is essentially a locked queue for communication and synchronization

    When there is no buffer, it is synchronous

  3. Receiving and sending of Chan

    1. Recvq is a blocked receiver queue, that is, in x = < – CH, when there is no data given to X in ch, the goroutine where x is located will be recorded in recvq, waiting for data
    2. Sendq is a blocked sender queue, that is, in Ch < – I, when the buffer of CH is full, the data will be put into sendq first. When the buffer is consumed, one will be copied from sendq to the buffer, and then the goroutine corresponding to the copied sendq will be released
    3. If there is a blocked receiver (recvq), the sending data will not be stored in the buffer (that is, it will not pass through Chan), but will directly copy the value to the address corresponding to X in x = < – Ch. the process is to determine whether the recvq of Chan is empty first, and if it is not empty, then directly copy the value to the goroutine corresponding to recvx
    4. So at most one of recvq and sendq has content
  4. For Chan with buffer and Chan without buffer, the difference in go processing is that when creating Chan with buffer, additional memory space will be opened to store the buffer

  5. After Chan is closed, the data in the buffer can be read

    Remember to close the goroutine that is written to ch, otherwise there will be unexpected panic

  6. Buffer of Chan

    1. The buffer of Chan is circular, that is to say, when sending or writing corner marker (recvx) is equal to the buffer length (dataqsiz), it will jump to the header (corner marker becomes 0)
    2. When sending corner = = writing corner, it is proved that it has been consumed
    3. When the number of elements in the buffer (qcount) = = the number of elements that the buffer can hold (dataqsiz), it is proved that the buffer is full

This work adoptsCC agreementReprint must indicate the author and the link of this article

Recommended Today

Third party calls wechat payment interface

Step one: preparation 1. Wechat payment interface can only be called if the developer qualification has been authenticated on wechat open platform, so the first thing is to authenticate. It’s very simple, but wechat will charge 300 yuan for audit 2. Set payment directory Login wechat payment merchant platform( pay.weixin.qq . com) — > Product […]