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!