当前位置:网站首页>Detailed instructions for channels
Detailed instructions for channels
2022-06-21 12:10:00 【attempt_ to_ do】
goroutine Run in the same address space , So access to shared memory must be synchronized . that goroutine How to communicate data between ,Go It provides a good communication mechanism channel.channel It can be done with Unix shell The two-way pipeline in the : It can send or receive values . These values can only be of a specific type :channel type . Define a channel when , Also need definition to send to channel The type of value of . Be careful , You have to use make establish channel:
ci := make(chan int)
cs := make(chan string)
cf := make(chan interface{
})
channel By the operator <- To receive and send data
ch <- v // send out v To channel ch.
v := <-ch // from ch Data received in , And assign it to v
package main
import "fmt"
func sum(a []int, c chan int) {
total := 0
for _, v := range a {
total += v
}
c <- total // send total to c
}
func main() {
a := []int{
7, 2, 8, -9, 4, 0}
c := make(chan int)
go sum(a[:len(a)/2], c)
go sum(a[len(a)/2:], c)
x, y := <-c, <-c // receive from c
fmt.Println(x, y, x + y)
}
By default ,channel Both receiving and sending data are blocked , Unless the other end is ready , So that makes Goroutines Synchronization becomes more simple , Without explicit lock. So called blocking , That is, if you read (value := <-ch) It will be blocked , Until data is received . secondly , Any sending (ch<-5) Will be blocked , Until the data is read . No buffer channel It's in multiple goroutine Great tools for syncing between .
Buffered Channels
Above we introduced the default non cache type channel, however Go It is also allowed to specify channel The buffer size of , It's simple , Namely channel How many elements can be stored .ch:= make(chan bool, 4), Created to store 4 An element of bool type channel. In this channel in , front 4 Elements can be written without blocking . When writing the 5 Element time , The code will block , Until something else goroutine from channel Read some elements , Make room .
ch := make(chan type, value)
When value = 0 when ,channel It's read and write without buffer , When value > 0 when ,channel There's a cushion 、 It's non blocking , Until writing is full. value Only elements block writing .
Range and Close
Go adopt range, Like operation slice perhaps map Same operation cache type channel.
package main
import (
"fmt"
)
func fibonacci(n int, c chan int) {
x, y := 1, 1
for i := 0; i < n; i++ {
c <- x
x, y = y, x + y
}
close(c)
}
func main() {
c := make(chan int, 10)
go fibonacci(cap(c), c)
for i := range c {
fmt.Println(i)
}
}
for i := range c Be able to read continuously channel The data in it , Until it's time to channel Is explicitly turned off . In the above code, we can see that it can be closed explicitly channel, Producers use built-in functions close close channel. close channel After that, no more data can be sent , In the consumer side you can use grammar v, ok := <-ch test channel Is it closed . If ok return false, It means that channel There is no data and it has been shut down .
Select
What we mentioned above is that there is only one channel The situation of , So if there are multiple channel When , How can we operate ,Go It provides a keyword select, adopt select Can monitor channel Data flow on .
select The default is blocked , Only when the monitor channel Only run when there is a send or receive available in , When more than one channel When it's all ready ,select It's a random choice to execute .
package main
import "fmt"
func fibonacci(c, quit chan int) {
x, y := 1, 1
for {
select {
case c <- x:
x, y = y, x + y
case <-quit:
fmt.Println("quit")
return
}
}
}
func main() {
c := make(chan int)
quit := make(chan int)
go func() {
for i := 0; i < 10; i++ {
fmt.Println(<-c)
}
quit <- 0
}()
fibonacci(c, quit)
}
stay select There are also default grammar ,select In fact, it's similar switch The function of ,default It's for monitoring channel When you're not ready , Default execution (select Wait no longer blocked channel).
select {
case i := <-c:
// use i
default:
// When c When it's blocked, execute here
}
Overtime
Sometimes there will be goroutine Blocking condition , So how can we prevent the whole program from blocking ? We can use select To set the timeout , This is achieved by :
func main() {
c := make(chan int)
o := make(chan bool)
go func() {
for {
select {
case v := <- c:
println(v)
case <- time.After(5 * time.Second):
println("timeout")
o <- true
break
}
}
}()
<- o
}
边栏推荐
- STM32开发之 VS Code + GDB下载调试
- HMS core machine learning service ID card identification function to achieve efficient information entry
- 架构师培养计划-无限思维——变量
- 华为是如何从0到1打造以项目为中心运作的项目管理体系的?
- 记一次Vmware虚拟机升级GLIBC导致系统瘫痪的恢复解决方法
- 6-zabbix monitors and automatically discovers the memory and CPU usage of third-party Middleware
- Travel does not heal the soul
- 旅行不能治愈心灵
- 2022 special operation certificate examination question bank and online simulation examination for safety management personnel of hazardous chemical business units
- i.MX - RT1052时钟及锁相环(PLL)分析
猜你喜欢

Customization of power aging test system | overview of charging pile automatic test system nsat-8000

Harmonyos training I
![[cloud native | Devops] Jenkins installation and actual combat (II)](/img/1f/68ec217a920fb7c737227137e3243c.png)
[cloud native | Devops] Jenkins installation and actual combat (II)

Jenkins 通过Build periodically配置定时任务

2-zabbix使用自动发现自动添加主机

Heavyweight, mapstruct 1.5 was released. This time, it finally supports the transformation of map into bean!

看懂UML类图和时序图

浅论OCA\UV-OCA LOCA\SLOCA 四种全贴合工艺

Hands on data analysis data reconstruction

20N10-ASEMI中低压MOS管20N10
随机推荐
Flink tuning (I) resource tuning and back pressure analysis
STM32開發之 VS Code + gcc環境編譯
版本号命名规范
100w的数据表比1000w的数据表查询更快吗?
[Harbin Institute of technology] information sharing for the first and second examinations of postgraduate entrance examination
Second harmonyos training
Compilation de l'environnement vs Code + GCC développé par stm32
2022年138套数学分析高等代数考研真题参考解答勘误
Musk's "good friend" impacts the largest IPO of Hong Kong stocks in 2022
Matrial3d parameter analysis
[yolov5s target detection] opencv loads onnx model for reasoning on GPU
Quantitative research on heterogeneous communities 4 rate of change with bands
华为是如何从0到1打造以项目为中心运作的项目管理体系的?
i.MX - RT1052 SPI和 I2C接口
马斯克的“好朋友”,冲击2022港股最大IPO
Jenkins 通过Build periodically配置定时任务
STM32笔记之 PWM(脉宽调制)
【yolov5s目标检测】opencv加载onnx模型在GPU上进行推理
typora免费版,无需破解,安装直接使用
[untitled]