The realization principle of channel in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. Channel is the mechanism of message passing for lock free synchronization in multi-threaded environments. It also has 2 features: 1. Message Delivery 2. Synchronous
Golang the performance of the channel, you can refer to the previous article: http://blog.sina.com.cn/s/blog_630c58cb01016xur.html in addition, the own runtime The benchmark code is already available in the package, and you can run the following command to see its performance: Go Test-v-test.bench= ". *" Runtime
The result on my PC is: benchmarkchanuncontended 50000000 67.3 ns/opbenchmarkchancontended 50000000 67.7 Ns/opbenchmarkchansync 10000000 181 NS/OPBENCHMARKCHANPRODCONS0 10000000 198 NS/OPBENCHMARKCHANPRODCONS10 20000000 98.2 ns/opbenchmarkchanprodcons100 50000000 73.4 Ns/opbenchmarkchanprodconswor K0 1000000 1874 Ns/opbenchmarkchanprodconswor K10 1000000 1805 Ns/opbenchmarkchanprodconswor K100 1000000 1771 Ns/opbenchmarkchancreation 10000000 195 Ns/opbenchmarkchansem 50000000 66.3 ns/op
The channel's implementation, all in the $GOROOT/SRC/PKG/RUNTIME/CHAN.C.
It is a struct that is implemented through shared memory Hchan {}
CH: = Make (Chan interface{}, 5) The specific implementation is CHAN.C hchan* runtime Makechan_c (Chantype *t, int64 hint) at this time, hint=5, t=interface{}

It accomplishes the task of allocating memory space for hint * sizeof (t) + sizeof (Hchan) [that is, the larger the buffer of buffered Chan, the greater the memory footprint]
CH <-5 will call void Runtime Chansend (Chantype *t, Hchan *chan, byte *ep, bool *pres) Lock (Chan) If Chan is a buffer chan { Compare whether the data currently in buffer is full a If not full { Copy the EP (data to be put into Chan) into Chan's memory area (this area is shared by Sender/recver) Find receiver Goroutine, make it ready, and schedule it to recv } else { It's full. Set the current goroutine state to Gwaiting Yield }
} else { This is blocked Chan. Find receiver Goroutine (the channel metaphor is that there must be multiple Goroutine) Turn the Goroutine into ready (previously gwaiting) to participate in schedule, gain control What to do depends on the realization of CHANRECV }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.