Home >Backend Development >Golang >The implementation mechanism of select in Golang
The following column golang tutorial will give you a detailed explanation of the implementation mechanism of select in Golang. I hope it will be helpful to friends in need!
#Text Speaking of playing today A problem was found when selecting, as follows: Fragment 1:func main(){ var count int for { select { case <-time.Tick(time.Millisecond * 500): fmt.Println("咖啡色的羊驼") count++ fmt.Println("count--->" , count) case <-time.Tick(time.Millisecond * 499) : fmt.Println(time.Now().Unix()) count++ fmt.Println("count--->" , count) } } }Fragment 2:
func main(){ t1 := time.Tick(time.Second) t2 := time.Tick(time.Second) var count int for { select { case <-t1: fmt.Println("咖啡色的羊驼") count++ fmt.Println("count--->" , count) case <-t2 : fmt.Println(time.Now().Unix()) count++ fmt.Println("count--->" , count) } } }Two questions:
1. The output of the above fragment turn out?
2.How to explain?
Fragment 1:
1535673600 count---> 1 1535673600 count---> 2 1535673601 count---> 3Fragment 2:
咖啡色的羊驼 count---> 1 1535673600 count---> 2 咖啡色的羊驼 count---> 3 1535673601 count---> 4The second one is easy to understand, because select monitors the two time channels, so they appear alternately.
So why only 1 appears in the first one?
In order to solve this problem, we have to revise the implementation mechanism of select, so we have this article.
1.select case is used to block the listening goroutine. If there is no case, just a select{} , it is to monitor the goroutine in the current program. At this time, please note that there needs to be a real goroutine running, otherwise select{} will report panic
func selectgo(sel *hselect) int { // ... // case洗牌 pollslice := slice{unsafe.Pointer(sel.pollorder), int(sel.ncase), int(sel.ncase)} pollorder := *(*[]uint16)(unsafe.Pointer(&pollslice)) for i := 1; i < int(sel.ncase); i++ { //.... } // 给case排序 lockslice := slice{unsafe.Pointer(sel.lockorder), int(sel.ncase), int(sel.ncase)} lockorder := *(*[]uint16)(unsafe.Pointer(&lockslice)) for i := 0; i < int(sel.ncase); i++ { // ... } for i := int(sel.ncase) - 1; i >= 0; i-- { // ... } // 加锁该select中所有的channel sellock(scases, lockorder) // 进入loop loop: // ... // pass 1 - look for something already waiting // 按顺序遍历case来寻找可执行的case for i := 0; i < int(sel.ncase); i++ { //... switch cas.kind { case caseNil: continue case caseRecv: // ... goto xxx case caseSend: // ... goto xxx case caseDefault: dfli = casi dfl = cas } } // 没有找到可以执行的case,但有default条件,这个if里就会直接退出了。 if dfl != nil { // ... } // ... // pass 2 - enqueue on all chans // chan入等待队列 for _, casei := range lockorder { // ... switch cas.kind { case caseRecv: c.recvq.enqueue(sg) case caseSend: c.sendq.enqueue(sg) } } // wait for someone to wake us up // 等待被唤起,同时解锁channel(selparkcommit这里实现的) gp.param = nil gopark(selparkcommit, nil, "select", traceEvGoBlockSelect, 1) // 突然有故事发生,被唤醒,再次该select下全部channel加锁 sellock(scases, lockorder) // pass 3 - dequeue from unsuccessful chans // 本轮最后一次循环操作,获取可执行case,其余全部出队列丢弃 casi = -1 cas = nil sglist = gp.waiting // Clear all elem before unlinking from gp.waiting. for sg1 := gp.waiting; sg1 != nil; sg1 = sg1.waitlink { sg1.isSelect = false sg1.elem = nil sg1.c = nil } gp.waiting = nil for _, casei := range lockorder { // ... if sg == sglist { // sg has already been dequeued by the G that woke us up. casi = int(casei) cas = k } else { c = k.c if k.kind == caseSend { c.sendq.dequeueSudoG(sglist) } else { c.recvq.dequeueSudoG(sglist) } } // ... } // 没有的话,再走一次loop if cas == nil { goto loop } // ... bufrecv: // can receive from buffer bufsend: // ... recv: // ... rclose: // ... send: // ... retc: // ... sclose: // send on closed channel }In order to facilitate the display, I specially made an ugly picture to illustrate the process: In other words, selection is carried out in four steps. The key point of doubt in this article is that in that loop, when an executable is found,
The channels corresponding to the cases that will not be executed in this selection will give the current goroutine of the team. Regardless of them, is lost. Since time.Tick is created on-site in the case, rather than in the global stack like fragment 2, every time any one is executed, the other one is abandoned. , when you select it again, you need to get it again, and it is new and you need to start over.
This is my current understanding. If you have a better understanding, please leave me a message. Thank you.The above is the detailed content of The implementation mechanism of select in Golang. For more information, please follow other related articles on the PHP Chinese website!