51CTO's no-sniff go basic tutorial concurrency-related concepts have errors

Source: Internet
Author: User

No-smell concurrency of the course looked down, and then gave him dizzy, book that, I was platinum poo Ah, temper is very excrement, you do not explain clearly, I go to figure out.

No, the buffer channel does not block, the non-cache channel will block, really?

No, I gave you an example.

Func Main () {C:=make (chan bool,1) Go Fun () {fmt.println ("go!") <-C} () C<-true}

The above code as long as the 1 is removed will output go! do not remove the output go! He says this proves that the buffer channel is not blocked and the non-cached channel is blocked. Well, then it won't superstitious, I'll take a piece of code

Testgo Project Main.gopackage mainimport ("FMT" "Time") Func main () {C:=make (chan bool,1) go func () { Fmt.        Println ("go!") <-c FMT.    PRINTLN ("123!") } () time. Sleep (1*time. Second) fmt. Println ("111") c<-true time. Sleep (1*time. Second) fmt. Println ("222") time. Sleep (2*time. Second)}

Let's guess, the output is BAA?

Go!

111

123!

222

Success: Process exit code 0.

Process Goroutines in the main process to establish a good pipeline C and the main process at the same time running, main first sleep for 1 seconds, first output goroutines go!, and then take the object from C, note yo, is a buffer pipe yo, according to no smell of the saying should not be blocked yo, Should direct output 123 yo, the result pinch? The toilet is blocked!!! Blocked!!! There is no object in the pipeline, can not be taken out, has been waiting for other processes to plug the object in the pipeline, so long as there is a pipeline, it will be blocked. In my example, the first output is 111, and so the main process to plug the object into the pipeline, the Goroutines process will not block, output 123.

The conclusion is that the cache channel is not blocked

Finally give everyone a long-term work since the advice, do not blindly believe in authority, do not know to try, the teacher said is not necessarily right, there is oneself if you really want to upload video tutorial, please first audit, or fraught AH

51CTO's no-sniff go basic tutorial concurrency-related concepts have errors

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.