Golang Channel has a significant difference between buffering and non-buffering

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.

There is a significant difference between buffer and unbuffered Golang channel.

I was naïve to think that there is a buffer and no buffer difference just unbuffered is the default buffer of 1 buffer

In fact, it's completely wrong, and there's a big difference between unbuffered and buffered channel.

That's one of the synchronizations that is a non-synchronous

What do you say? Like what

C1:=make (chan int) unbuffered

C2:=make (Chan int,1) has a cushion

C1<-1

Unbuffered not only to the C1 Channel 1 but always have to have other Ctrip <-C1 took over this parameter, then c1<-1 will continue, otherwise it has been blocked

The c2<-1 is not blocked because the buffer size is 1 (in fact the buffer size is 0) only when the second value is not taken away from the first one, it will only block.

Make a metaphor.

No buffer is a messenger to your door to send letters, you are not at home he does not go, you must take the letter, he will go.

No buffer guarantee to get your hands on the letter.

There is a buffer is a messenger to your home still to your home mailbox turned away, unless your mailbox is full he must wait for the mailbox empty.

There is a buffer guarantee to enter your home mailbox.

++++++++++++++++++++++++++++++++++++++

"I concluded that" the non-buffered channel is blocked until it can get data from Chan, and the buffered channel is blocking the code that executes the main thread as long as it can fetch one!

Related Article

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.