Blocking test of go channel

go version

go version go1.11.2 linux/amd64

Unbuffered channel

Test example

package main

import "fmt"

func main(){
naturals:=make(chan int,0)//No cache channel
squares:=make(chan int)
go func(){
    for x:=0;x<10;x++{
        fmt.Println("naturals start ",x)
        naturals<-x
        fmt.Println("naturals end ",x)
    }
     fmt.Println("naturals close ")
    close(naturals)
}()

go func(){
    for x:=range naturals{
        fmt.Println("squares start ",x)
        squares<-x
        fmt.Println("squares end ",x)
    }
     fmt.Println("squares close ")
    close(squares)
}()

for x:=range squares{
    fmt.Println("main ",x)
}


}

output

naturals start  0
naturals end  0
naturals start  1
squares start  0
squares end  0
squares start  1
naturals end  1
naturals start  2
main  0
main  1
squares end  1
squares start  2
squares end  2
main  2
naturals end  2
naturals start  3
naturals end  3
naturals start  4
squares start  3
squares end  3
squares start  4
naturals end  4
naturals start  5
main  3
main  4
squares end  4
squares start  5
squares end  5
main  5
naturals end  5
naturals start  6
naturals end  6
naturals start  7
squares start  6
squares end  6
squares start  7
naturals end  7
naturals start  8
main  6
main  7
squares end  7
squares start  8
squares end  8
main  8
naturals end  8
naturals start  9
naturals end  9
naturals close 
squares start  9
squares end  9
squares close 
main  9

summary

The point is at the beginning

naturals start  0
naturals end  0
naturals start  1
squares start  0

The unbuffered channel can insert a message, and then continue to move down,
If the message has not been received and continues to be sent, it will cause blocking and switch to another goroutine

Buffered channel

Test example

package main

import "fmt"

func main(){


naturals:=make(chan int,1)//Cache channel capacity is 1
squares:=make(chan int)
go func(){
    for x:=0;x<10;x++{
        fmt.Println("naturals start ",x)
        naturals<-x
        fmt.Println("naturals end ",x)
    }
     fmt.Println("naturals close ")
    close(naturals)
}()

go func(){
    for x:=range naturals{
        fmt.Println("squares start ",x)
        squares<-x
        fmt.Println("squares end ",x)
    }
     fmt.Println("squares close ")
    close(squares)
}()

for x:=range squares{
    fmt.Println("main ",x)
}


}

output

naturals start  0
naturals end  0
naturals start  1
naturals end  1
naturals start  2
squares start  0
squares end  0
squares start  1
naturals end  2
naturals start  3
main  0
main  1
squares end  1
squares start  2
squares end  2
squares start  3
main  2
main  3
squares end  3
naturals end  3
naturals start  4
naturals end  4
naturals start  5
naturals end  5
naturals start  6
squares start  4
squares end  4
squares start  5
naturals end  6
naturals start  7
main  4
main  5
squares end  5
squares start  6
squares end  6
squares start  7
main  6
main  7
squares end  7
naturals end  7
naturals start  8
naturals end  8
naturals start  9
naturals end  9
naturals close 
squares start  8
squares end  8
squares start  9
main  8
main  9
squares end  9
squares close 

summary

The point is at the beginning

naturals start  0
naturals end  0
naturals start  1
naturals end  1
naturals start  2
squares start  0

The buffered channel with capacity of 1 can insert two messages, and then continue to move down,
If these two messages have not been received and continue to send messages, it will cause blocking and switch to another goroutine

Tags: Go Linux

Posted on Wed, 04 Dec 2019 04:23:03 -0500 by katuki