Home  >  Article  >  Backend Development  >  An article to help you understand the basics of Go language concurrency (channel)

An article to help you understand the basics of Go language concurrency (channel)

Go语言进阶学习
Go语言进阶学习forward
2023-07-21 10:09:541411browse
Why do we need channel

channel in Go, also called a pipeline, is used to share data# between multiple threads ##of. Normally, shared data in Go also uses

channel, but there are two a way to share data.

Shared memory implements communication.
  • #Communicate through channels (recommended).

Why is shared memory communication not recommended?

Sample code: Multi-threading modifies a value.

Function

func Calc() {
    defer wg.Done()
    NUM = NUM - 1
}

main

var NUM = 100
var wg sync.WaitGroup


func main() {
    for i := 0; i<100;i++  {
        wg.Add(1)
        go Calc()
}
    wg.Wait()
    fmt.Println(NUM)
}

Execution result

An article to help you understand the basics of Go language concurrency (channel)

Yes, it is 2, you are confused, hahaha, the theory should be 0 ah.

Why is this?

This is why shared memory is not recommended. Our code is already multi-threaded.

In the first function code , line 3, NUM = NUM ​​- 1.

如果多个线程同时执行到这一行,并且没有加锁,就会出现数据错乱。

那该怎么做呢?

加锁,加锁可以保证某一段代码只能被一个线程执行,防止被争抢。

代码

func Calc() {
    defer wg.Done()
    mutex.Lock()
    NUM = NUM - 1
    mutex.Unlock()
}

第3行加锁,第5行解锁

执行结果

An article to help you understand the basics of Go language concurrency (channel)

这次真的是0的,不管执行几次。

但是会发现一个问题,如果采用这种方式,需要常常注意竞争问题。

所以不是太推荐,需要考虑的比较多,并且各种加锁会消耗性能。

channel语法

channel格式

var 变量名 chan 类型
例如
var x1 chan int //x1管道里面只能存int类型数据
var x2 chan string //x2管道里面只能存字符串类型数据

注意

An article to help you understand the basics of Go language concurrency (channel)

定义管道时,chan int是一个整体,别搞错了各位。

创建channel

创建channel,只能通过make创建。

格式

var 变量名 = make(chan 类型,[管道大小])
示例
var chan1 = make(chan int,10)//管道可以放10个int元素
var chan2 = make(chan string,5)//管道可以放5个string元素

channel操作

创建一个管道。

ch = make(chan int,10)

channel是一个管道,就像一个管子。

所以可以像管子里面塞东西,并且能取东西关闭管道就是这个管道不能用了,里面的值取完就打样了

像管子塞东西(发送)ch <- 666

从管子取东西(接收)var x = <- ch

Close the tubeclose(ch).

Note: channel is a first-in-first-out structure ,like this.

An article to help you understand the basics of Go language concurrency (channel)

Precautions:

  • If the channel is full, it will be blocked if it is blocked again.

  • #If the channel is closed, no more value can be added, otherwise it will panic.

  • #Even if the channel is closed, the value can still be obtained until the value of the pipe is completely obtained. After the value is obtained, the zero value of the corresponding type is obtained.

  • The pipe cannot be closed repeatedly. Repeated closing will panic.


Unbuffered pipe

Unbuffered means that the pipe has no length. like this.

就像快读员没有快递柜,需要直接将快递给客户,如果没人要就撂摊子。

An article to help you understand the basics of Go language concurrency (channel)

示例代码

package main


import (
    "fmt"
)


//模拟张三
func 张三(x chan string) {
    var a = <-x
    fmt.Println(a)
}


func main() {
    //通道没有长度,就是无缓冲通道
    var x = make(chan string)
    go 张三(x)
    x <- "张三的快递"
    fmt.Println("张三快递交付成功")
}

第16行写入一个值,同理,张三就要等着去接,如果没人接,那就完了。

假设注释第9行代码。

An article to help you understand the basics of Go language concurrency (channel)

直接报错,all goroutines are asleep - deadlock!,这句话的意思是所有的协程都睡着了,死锁

无缓冲说明通道长度为0发送一个值会阻塞住

这就相当于快递员直接找张三,但是张三没了,但是快递员还得一直等着,等等等,然后挂了,终究还是没送出去。

有缓冲管道

An article to help you understand the basics of Go language concurrency (channel)

这个就简单啦,多了一个快递柜,快递员直接将快递仍快递柜就行了。

示例代码

package main


import (
    "fmt"
    "sync"
)


var wg sync.WaitGroup


//快递员,快递员放10个快递
func 快递员(kuaidigui chan string) {
    defer wg.Done()
    for i := 0; i < 10; i++ {
        fmt.Println("快递员放入了第",i,"快递")
        kuaidigui <- fmt.Sprintf("第%d个快递", i)
}
    //放完快递就关闭了通道
    close(kuaidigui)
}


//张三,拿走3个快递
func 张三(kuaidigui chan string) {
    defer wg.Done()
    for i := 0; i < 3; i++ {
        fmt.Println("张三拿走" + <-kuaidigui)
}
}
//李四拿走7个快递
func 李四(kuaidigui chan string) {
    defer wg.Done()
    for i := 0; i < 7; i++ {
        fmt.Println("李四拿走" + <-kuaidigui)
}
}
func main() {
    //快递柜,10个大小
    var 快递柜 = make(chan string, 10)
    wg.Add(3)
    go 快递员(快递柜)
    go 张三(快递柜)
    go 李四(快递柜)
    wg.Wait()
}

执行结果

An article to help you understand the basics of Go language concurrency (channel)

遍历channel两种方式

代码

func main() {
    //快递柜,10个大小
    var ch = make(chan int, 10)
    //向管道中发送值
    for i := 0; i < 10; i++ {
        ch <- i
}
    //方式一取值
    //for {
    //i, ok := <-ch
    ////取完值ok就是false
    //if !ok {
    //      //结束循环
    //      break
    //}
    //fmt.Println(i)
    //}
    //方式二取值
    for i:=range ch{
        fmt.Println(i)
}
}

执行结果

An article to help you understand the basics of Go language concurrency (channel)

报错是因为我在main中完成了发送值和取值两个操作,所以会出现上述问题,但是结果是没有错的。

One-way channel

We know that channel is possible Sending values ​​ and take the value , but in some scenarios for safety reasons, theoretically only Get the value, the latter can only send the value.

One-way channels are usually only reflected in function parameters.

  • Formal parameter chan<- chan typeWriting only.

  • Formal parameters <-chan chan typeRead only.

#Modify the above courier code.

package main


import (
    "fmt"
    "sync"
)


var wg sync.WaitGroup


//快递员,快递员放10个快递,只写 chan<- string
func 快递员(kuaidigui chan<- string) {
    defer wg.Done()
    for i := 0; i < 10; i++ {
        fmt.Println("快递员放入了第", i, "快递")
        kuaidigui <- fmt.Sprintf("第%d个快递", i)
}
    //放完快递就关闭了通道
    close(kuaidigui)
}


//张三,拿走3个快递,只读<-chan string
func 张三(kuaidigui <-chan string) {
    defer wg.Done()
    for i := 0; i < 3; i++ {
        fmt.Println("张三拿走" + <-kuaidigui)
}
}


//李四拿走7个快递
func 李四(kuaidigui <-chan string) {
    defer wg.Done()
    for i := 0; i < 7; i++ {
        fmt.Println("李四拿走" + <-kuaidigui)
}
}
func main() {
    //快递柜,10个大小
    var 快递柜 = make(chan string, 10)
    wg.Add(3)
    go 快递员(快递柜)
    go 张三(快递柜)
    go 李四(快递柜)
    wg.Wait()
}

总结

上述讲述了Go语言并发如何和channel配合使用,毕竟我们一般的任务都不是单独运行的,都是互相配合的。

我们讲述了如何创建channel如何使用channel有缓冲管道和无缓冲管道区别,并且拒了一个快递员例子来展示协程和channel如何配合,最后用单向通道又加固了一下代码。

我的代码中使用了中文命名变量名是为了好看,实际开发中千万不要这样!!!

The above is the detailed content of An article to help you understand the basics of Go language concurrency (channel). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Go语言进阶学习. If there is any infringement, please contact admin@php.cn delete