golang的Channel
golang的Channel
Channel 是 golang 一个非常重要的概念,如果你是刚开始使用 golang 的开发者,你可能还没有真正接触这一概念,本篇我们将分析 golang 的Channel
1. 引入
要讲 Channel 就避不开 Goroutine -- 协程。闲话不说, 直接上个例子
Goroutine demo:
package main
import (
"fmt"
"time"
)
func main() {
origin := 1
go doSomeCompute(origin)
time.Sleep(5 * time.Second)
}
func doSomeCompute(num int) {
result := num * 2
fmt.Println(result)
return
}
简单来说,例子中有一个 main 的协程,一个 doSomeCompute 的协程。还有个延时退出的方法等待计算协程计算结果。我们尝试思考这个例子的一些问题:
a. 如何获取 doSomeCompute 的计算结果?
b. 如何获取 doSomeCompute 的执行状态,当前是执行完了还是执行中?
如何解决这种问题呢?
Channel!
2. Channel
Channel怎么处理上面的问题?我们直接上代码:
举例
package main
import (
"fmt"
)
func main() {
origin := 1
//一个无缓冲Channel
res := make(chan int)
go doSomeCompute(origin, res)
fmt.Println(<-res)
}
func doSomeCompute(num int, res chan int) {
result := num * 2
res <- result
}
例子中,Channel 充当起了协程间通信的桥梁。Channel 可以传递到协程说明它是线程安全的,事实也是如此。Channel 可以理解为管道,协程 doSomeCompute 向 Channel 写入结果, main 中读取结果。注意,例子中读取 Channel 的地方会阻塞直到拿到计算结果,这样就解决了问题 a 和 b。
2. Channel 的方向性
上面的例子中,计算协程是负责计算并将计算结果写入 Channel ,如果我们希望保证计算协程不会从 Channel 中读取数据该怎么处理?很简单,看例子:
func doSomeCompute(num int, res chan<- int) {
result := num * 2
res <- result
}
这个参数的声明 chan<- int 就表示该函数只能讲数据写入 Channel ,而不能从中读取数据。后面的 int 表示 Channel 中数据的格式。同样的, 只可以读取数据的 Channel 可以声明为 <-chan int 。而例子中不带有方向声明的 Channel 则既可以写入也可以读取。
3. 阻塞性质
Channel 的读取和写入操作在各自的协程内部都是阻塞的。比如例子中 fmt.Println(<-res) , 这一语句会阻塞直至计算协程将计算结果放入,可以读出。也就是说,协程会阻塞直至从 res 中读出数据。
注意,无缓冲的 Channel 的读写都是阻塞的,有缓冲的 Channel 可以一直向里面写数据,直到缓存满才会阻塞。读取数据同理, 直至 Channel 为空才阻塞。
用一个典型的例子来说明缓冲和非缓冲 Channel 的区别:
package main
import "fmt"
func doSomeCompute(ch chan int) {
fmt.Println("deadlock test")
<- ch
}
func main() {
ch := make(chan int)
ch <- 1
go doSomeCompute(ch)
}
例子中,main 协程会向 ch 写入数据, 这一过程是阻塞的,也就是说,doSomeCompute 协程无法执行,程序死锁。输出如下:
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]:
main.main()
C:/mygo/src/demo/blog.go:12 +0x73
exit status 2
如果改成有缓冲的 Channel :
package main
import (
"fmt"
"time"
)
func doSomeCompute(ch chan int) {
fmt.Println("deadlock test")
<- ch
}
func main() {
ch := make(chan int, 1)
ch <- 1
go doSomeCompute(ch)
time.Sleep(1 * time.Second)
}
有与有缓冲的 Channel 写入后不阻塞(下一次写入才会阻塞),程序会继续执行。
4. Channel 的数据结构
Channel 在 golang 中实际上就是个数据结构。在 Golang 源码中, Channel 的数据结构 Hchan 的定义如下:
struct Hchan
{
uint32 qcount; // total data in the q
uint32 dataqsiz; // size of the circular q
uint16 elemsize;
bool closed;
uint8 elemalign;
Alg* elemalg; // interface for element type
uint32 sendx; // send index
uint32 recvx; // receive index
WaitQ recvq; // list of recv waiters
WaitQ sendq; // list of send waiters
Lock;
};
时间仓促,这次对 Channel 介绍写的有点简单粗暴,下次再写。
golang的Channel的更多相关文章
- c#实现golang 的channel
使用.NET的 BlockingCollection<T>来包装一个ConcurrentQueue<T>来实现golang的channel. 代码如下: public clas ...
- golang的channel实现
golang的channel实现位于src/runtime/chan.go文件.golang中的channel对应的结构是: // Invariants: // At least one of c.s ...
- 【GoLang】golang context channel 详解
代码示例: package main import ( "fmt" "time" "golang.org/x/net/context" ) ...
- 深入学习golang(2)—channel
Channel 1. 概述 “网络,并发”是Go语言的两大feature.Go语言号称“互联网的C语言”,与使用传统的C语言相比,写一个Server所使用的代码更少,也更简单.写一个Server除了网 ...
- golang中channel的超时处理
并发中超时处理是必不可少的,golang没有提供直接的超时处理机制,但可以利用select机制来解决超时问题. func timeoutFunc() { //首先,实现并执行一个匿名的超时等待函数 t ...
- 如何优雅的关闭golang的channel
How to Gracefully Close Channels,这篇博客讲了如何优雅的关闭channel的技巧,好好研读,收获良多. 众所周知,在golang中,关闭或者向已关闭的channel发送 ...
- golang 中 channel 的非阻塞访问方法
在golang中,基本的channel读写操作都是阻塞的,如果你想要非阻塞的,可以使用如下示例: 即只要在select中加入default,阻塞立即变成非阻塞: package main import ...
- golang查看channel缓冲区的长度
golang提供内建函数cap用于查看channel缓冲区长度. cap的定义如下: func cap(v Type) int The cap built-in function returns th ...
- Golang 入门 : channel(通道)
笔者在<Golang 入门 : 竞争条件>一文中介绍了 Golang 并发编程中需要面对的竞争条件.本文我们就介绍如何使用 Golang 提供的 channel(通道) 消除竞争条件. C ...
随机推荐
- css 2D转换 transform-rotate 画插图
学习了一点2D转换,关于Transfrom-rotate的小用法 rotate()方法,在一个给定度数顺时针旋转的元素.负值是允许的,这样是元素逆时针旋转. 下面看实例 第一个例子是没有使用rotat ...
- 或许你不知道的10条SQL技巧(转自58沈剑原创)
这几天在写索引,想到一些有意思的TIPS,希望大家有收获. 一.一些常见的SQL实践 (1)负向条件查询不能使用索引 select * from order where status!=0 and s ...
- 解决React Native unable to load script from assets index.android.bundle on windows
React Native运行的时候,经常碰到React Native unable to load script from assets index.android.bundle on windows ...
- Codeforces_499C:Crazy Town(计算几何)
题目链接 给出点A(x1,y1),B(x2,y2),和n条直线(ai,bi,ci,aix + biy + ci = 0),求A到B穿过多少条直线 枚举每条直线判断A.B是否在该直线两侧即可 #incl ...
- Linux常用命令和常见问题解决<------>第一章
查看文件下面所有的隐藏目录:ls -al ~ ls -al ~ls -a -l ~可以发现三条命令执行结果是一致的,原因:因为ls为命令 后面的参数要以空格来区分,不论几个空格 shell都会视为一体 ...
- perl的INC设置
perl中的INC变量中包含了所有的perl module的查找路径. 可以使用perl -V 来查看INC的值. 1)默认地INC如下: @INC: C:/Perl/site/lib C ...
- HDU1036 Average is not Fast Enough!
Problem Description A relay is a race for two or more teams of runners. Each member of a team runs o ...
- HDU1027 Ignatius and the Princess II
Problem Description Now our hero finds the door to the BEelzebub feng5166. He opens the door and fin ...
- python3网络编程之socketserver
本节主要是讲解python3网络编程之socketserver,在上一节中我们讲到了socket.由于socket无法支持多用户和多并发,于是就有了socket server. socket serv ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...