The implementation of channel channel in Golang in several typical languages

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

With Golang for a while, the pipeline in Golang is really an artifact, and we use producer-consumer to describe its basic usage as:

Func P (Queue chan<-int) {for    i:= 0; i <; i++{        queue <-i    }}func C (Queue <-chan int) {    V: = <-Queue}func Main () {     queue: = Make (chan int, ten)  //Here the 10 represents the capacity of the pipeline, set according to the needs of the application     go P (queue)     go C ( Queue)}


The purpose of the pipeline in the example above is to add data to the pipeline, and when the pipeline is filled (10 data in the example above), the producer blocks (waiting for consumer consumption); when there is data in the pipeline, the consumer gets the data from the pipeline, otherwise it blocks (waiting for the producer to add data to it).

Obviously, Golang's message-based mode greatly simplifies the above requirements, and it is cumbersome to implement locks and condition variables in other languages.

Because this scene is used in almost all projects, recently specifically implemented C + +, Java, C # Three version, GitHub address: Https://github.com/ChenJohnson/Channel, welcome to use, if there are errors, Please feedback in time, thank you!




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.