Author: Regan Yue
Source: Hang Seng LIGHT cloud community
Understanding the cache capacity of the pipeline in the Golang process
Hello everyone, here is Mr. R, who is striving to become excellent. This time we will continue the Golang series let's Golang together. The content of the blockchain series will continue to be updated next year. The consensus algorithm has been basically completed. If there is no accident, we will introduce how to build a public chain project in the week of new year's day. This time we will learn about the cache capacity of the pipeline, This is an easy to understand knowledge point. Let's have a look!
As we mentioned earlier, when using make to establish a pipeline, the second parameter is zero, which proves that the pipeline is a pipeline without cache capacity. As long as no one writes, you can never read it, and as long as no one reads, you can never write it. For example:
ch := make(chan int,0)
The buffer of the pipeline can be initialized to the specified buffer capacity. If it is zero or the size is omitted, the channel is unbuffered.
If the second parameter is changed to 8 (it can be any size here), it means that the cache capacity is 8 and 8 elements can be written even if they are not read.
package main import ( "fmt" "time" ) func main() { //The channel's buffer is initialized with the specified buffer capacity. //If zero, or the size is omitted, the channel is unbuffered. ch := make(chan int,0) go func() { ch <- 123 fmt.Println("Data written") }() go func() { time.Sleep(2*time.Second) x:= <- ch fmt.Println("Data read out",x) }() time.Sleep(5*time.Second) fmt.Println("Game Over!") }
During the operation of this code, because one process is writing to the pipeline buffer and the other process is reading the pipeline buffer, but the process that reads the pipeline buffer will sleep for two seconds, so the other process that writes to the pipeline buffer in the first two seconds cannot be written.
If you let the one that writes to the pipeline buffer sleep for two seconds, the other one that reads the pipeline buffer in the first two seconds cannot read data.
If a pipeline with a buffer size of 3 writes four values, the fourth value cannot be written. The operation result is as follows:
Write 1 Write 2 Write 3
Let's take a look at the number of elements in the pipeline and its cache capacity:
package main import ( "fmt" "time" ) func main101() { ch := make(chan int,3) go func() { ch <- 1 fmt.Println("Write 1") ch <- 2 fmt.Println("Write 2") ch <- 3 fmt.Println("Write 3") //The buffer of the pipeline is full and cannot be written again! ch <- 4 fmt.Println("Write 4") }() time.Sleep(5 * time.Second) } func main() { ch := make(chan int,3) fmt.Println("The number of elements is",len(ch),"Cache capacity is",cap(ch)); ch <- 123 fmt.Println("The number of elements is",len(ch),"Cache capacity is",cap(ch)); ch <- 123 fmt.Println("The number of elements is",len(ch),"Cache capacity is",cap(ch)); ch <- 123 fmt.Println("The number of elements is",len(ch),"Cache capacity is",cap(ch)); }
The result is
The number of elements is 0 and the cache capacity is 3 The number of elements is 1 and the cache capacity is 3 The number of elements is 2 and the cache capacity is 3 The number of elements is 3 and the cache capacity is 3 fatal error: all goroutines are asleep - deadlock! goroutine 1 [chan send]: main.main() E:/main.go:36 +0x4e5 Process finished with exit code 2
We can see that the cache capacity of the pipeline does not change, but the number of elements stored is changing.
We can see that there is a deadlock, where the main process can never continue to execute.
Let's learn more about pipeline.
It is understood that the pipeline is built on the heap. Using the make function returns pointers, which is why we can pass pipes between functions without passing pointers to pipes.
ch := make(chan int,0)
Using make to create a pipeline can only transmit the same type of data. When creating a pipeline, you need to specify a data type. Multiple types of data are not allowed to be transmitted through a pipeline.
Want to learn more from technology bosses? Where to discuss the problems encountered in development? How to obtain massive financial technology resources?
Hang Seng LIGHT cloud community , the financial technology professional community platform built by Hang Seng electronics shares practical technology dry goods, resource data and financial technology industry trends, and embraces all financial developers.
Scan the QR code of the applet below and join us!