Home  >  Article  >  Backend Development  >  Detailed explanation of Golang channel Chan

Detailed explanation of Golang channel Chan

藏色散人
藏色散人forward
2020-11-09 15:40:542614browse

The following is a detailed introduction to Golang from the golang tutorial column about the channel Chan. , I hope it will be helpful to friends in need!

Detailed explanation of Golang channel Chan

First let’s look at threads, also called goroutine in golang

Before reading this article, we need to understand concurrency and parallelism. Golang's thread is a concurrency mechanism, not parallelism. You can search the Internet for the differences between them. There are many introductions online.

Let’s take a look at an example first

import(
         "fmt"
)

funcmain(){

    go fmt.Println("1")
    fmt.Println("2")    
}

In golang, you can create a thread by using the go keyword followed by a function. The following function can be an already written function, or it can be an anonymous function

funcmain(){    var i=3    go func(a int) {        fmt.Println(a)
        fmt.Println("1")
    }(i)
    fmt.Println("2")}

The above code creates an anonymous function, and also passes in a parameter i, which is in parentheses below The i is the actual parameter and a is the formal parameter.

So can the above code print 1, 2, and 3 as we expected? Let me tell you, no, the program can only print out 2. I'll post the correct code below

import(    "fmt"
    "time"    )funcmain(){    var i = 3    go func(a int) {        fmt.Println(a)
        fmt.Println("1")
    }(i)
    fmt.Println("2")
    time.Sleep(1 * time.Second)}

I just added a line of code at the end to make the main thread sleep for one second, and the program will print out 2, 3, and 1 in sequence

.
So why is this happening? Because the program will execute the main thread first, after the execution of the main thread is completed, the program will exit immediately, leaving no extra time to execute the child thread. If you let the main thread sleep for 1 second at the end of the program, the program will have enough time to execute the child thread.

The thread is here first, let’s take a look at the channel.

Channel is also called channel. As the name suggests, the function of channel is to transfer data between multiple threads.

Create an unbuffered channel

chreadandwrite :=make(chan int)

chonlyread := make(<-chan int) //Create a read-only channel
chonlywrite := make(chan<- int) //Create a write-only channel
Let's look at an example:

    ch :=make(chan int)     
    ch <- 1
      go func() {
        <-ch
        fmt.Println("1")
      }()
      fmt.Println("2")

An error will occur when this code is executed: fatal error: all goroutines are asleep - deadlock!

This error means that the thread is stuck in a deadlock and the program cannot continue to execute. So what is the cause of this error?

We created an unbuffered channel and then assigned a value to the channel. The program fell into a deadlock after the assignment was completed. Because our channel is unbuffered, that is, synchronous, the program will be blocked before the channel can be read after the assignment is completed. Here is a very important concept: the channel mechanism is first-in-first-out. If you assign a value to the channel, you must read its value, otherwise it will cause blocking. Of course, this is only valid for unbuffered channels. For buffered channels, the sender will block until the data is copied to the buffer; if the buffer is full, the sender can only recover from the blocking state after the receiver removes the data.

There are two solutions to the above example:

1. Add a buffer to the channel, and then let the main thread sleep for one second at the end of the program. The code is as follows:

    ch :=make(chan int,1)
    ch <- 1
    go func() {
        v := <-ch
        fmt.Println(v)
    }()
    time.Sleep(1 * time.Second)
    fmt.Println("2")

In this case, the program will print out 1 and 2 in sequence

2. Put the ch<-1 line of code behind the sub-thread code. The code is as follows:

    ch :=make(chan int)    go func() {
        v := <-ch
        fmt.Println(v)
    }()
    ch <- 1
    fmt.Println("2")

It is not needed here. Let the main thread sleep, because after the channel is assigned a value in the main thread, the main thread will block until the value of the channel is taken out in the child thread.

Finally, let’s look at an example of a producer and a consumer:

import (    "fmt"
    "time")func produce(p chan<- int) {    for i := 0; i < 10; i++ {
        p <- i
        fmt.Println("send:", i)
    }
}func consumer(c <-chan int) {    for i := 0; i < 10; i++ {
        v := <-c
        fmt.Println("receive:", v)
    }
}func main() {
    ch := make(chan int)    go produce(ch)    go consumer(ch)
    time.Sleep(1 * time.Second)
}

In this code, because the channel is not buffered, when the producer assigns a value to the channel, the producer The thread will block until the consumer thread takes out the data from the channel. After the consumer takes out the data for the first time, the consumer's thread will also be blocked during the next cycle because the producer has not yet stored the data. At this time, the program will execute the producer's thread. In this way, the program continuously switches between the consumer and producer threads until the loop ends.

Let’s look at another example with buffering:

import (    "fmt"
    "time")func produce(p chan<- int) {    for i := 0; i < 10; i++ {
        p <- i
        fmt.Println("send:", i)
    }
}func consumer(c <-chan int) {    for i := 0; i < 10; i++ {
        v := <-c
        fmt.Println("receive:", v)
    }
}func main() {
    ch := make(chan int, 10)    go produce(ch)    go consumer(ch)
    time.Sleep(1 * time.Second)
}

In this program, the buffer can store 10 integers of type int. When executing the producer thread, the thread will not It will block and store 10 integers into the channel at one time. When reading, it is also read at one time.

The above is the detailed content of Detailed explanation of Golang channel Chan. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete