There are cache channels and no cache channel differences in Go

Source: Internet
Author: User
This is a creation in Article, where 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 only when the second value is put in the first one has not been taken away, this time will be blocked.

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.

See the test code

There's a cushion.

1Package Main23 Import"Fmt"45var c = Make (chanInt1)  6  7 func f () { 9 c <- Span style= "color: #800000;" > ' 
     "c"     <-c<-C,FMT. Println (" external call ")            

Non-buffered

//PpPackage Mainimport ("Fmt") func Writeroutine (Test_chan Chanint, valueInt) {Test_chan <-Value}func Readroutine (Test_chan Chaninttest_chan Span style= "color: #0000ff;" >return}func Main () {c: = Make (chan int100 ////go Writeroutine (c, x) //writeroutine (c, x) // go Readroutine (c) //go Readroutine (c) //writeroutine (c, x)  go Writeroutine (c, x)             

  
    Go Writeroutine (c, x)    Readroutine (c)    FMT. PRINTLN (x)}

Can be commented out in part of the code, see the effect, and then experience the next

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.