当前位置:网站首页>Go uses channel for synchronization (buffer channel)
Go uses channel for synchronization (buffer channel)
2022-04-21 10:42:00 【genispan】
The last article mentioned the common channel Used for thread synchronization .Go There is also a buffer in the language channel.
ch := make(chan int, 100)
The execution type and buffer size are required .
package main
import "fmt"
func main() {
c := make(chan int, 2) // establish int The type size is 2 The buffer
c <- 1 // buffer 1 The assignment is 1
c <- 2 // buffer 2 The assignment is 2
fmt.Println(<-c) // Output buffer 1
fmt.Println(<-c) // Output buffer 2
}
It can cycle for i := range c To keep coming from channel Receives the value , Until it's turned off .
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)+2, c)
for i := range c {
fmt.Println(i)
}
}
版权声明
本文为[genispan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211042160198.html
边栏推荐
- Moore thread cooperates with ampere computing
- 2022 information and future preparation topic 4 new online judge 2034: pruning shrubs
- 趣丸集团招股书“失效”,旗下TT语音已下架,如何实现稳定增长?
- AcWing 1737. 传送(分类讨论)
- HMS Core 6.4.0版本发布公告
- MKL and vs2019 configuration method
- GO 使用channel进行同步 (缓冲channel)
- TypeError: The view function did not return a valid response. The function either returned None 的解决
- AOT和单文件发布对程序性能的影响
- Wxml template syntax - data binding
猜你喜欢

Pytorch学习笔记(3)torch.autograd,逻辑回归模型训练

Pytorch学习笔记(2)一元线性回归、计算图示例

postman设置环境变量,简单又实用

Convbert: improving Bert with span based dynamic revolution

背包问题小结(0-1,完全,多重背包问题)

塔米狗知识|上市公司收购的基本原则

活动精彩预告 | 维塔士+龙智:数字化打造游戏行业「头号玩家」

2022 information and future preparation topic 2 new online judge 1113: digit problem

趣丸集团招股书“失效”,旗下TT语音已下架,如何实现稳定增长?

TypeError: The view function did not return a valid response. The function either returned None 的解决
随机推荐
基于润和大禹开发板的导购系统项目方案
MKL与VS2019配置方法
Map与JsonObject区别
MySQL深入学习(三十):数据库的设计规范
塔米狗企业并购平台上,法律服务商名单目录!
24张图攻克border-image
PHP文件包含:require,require_once;include,include_once
C语言 字符串
Usage of SAP ABAP for all entries
JS initial practice -- an example of dealing with the collision between a pinball and a wall
塔米狗知识|上市公司收购的基本原则
【acwing】1459. 奶牛体操(模拟、思维)
Basic commands of MySQL
Can Jingdong Logistics, ririshun supply chain and Shunfeng find the optimal solution of logistics under the "epidemic"?
shell脚本修改文件名和权限
Local IP addresses are accessed using domain names
GO语言反射机制
Digital Economy & Green Innovation matching: Green Patent matching data of digital finance enterprises of 3169 companies in China, 2011-2019
【pytorch】实现1对多的IOU计算技巧
2-3. Register selector