本文的主要内容是:算法
- 了解goroutine,使用它来运行程序
- 了解Go是如何检测并修正竞争状态的(解决资源互斥访问的方式)
- 了解并使用通道chan来同步goroutine
Go的并发能力,是指让某个函数独立于其余函数运行的能力。当为一个函数建立goroutine
时,该函数将做为一个独立的工做单元,被 调度器 调度到可用的逻辑处理器上执行。Go的运行时调度器是个复杂的软件,它作的工做大体是:缓存
参考The Go scheduler ,这里较浅显地说一下Go的运行时调度器。操做系统会在物理处理器上调度操做系统线程
来运行,而Go语言的运行时会在逻辑处理器
上调度goroutine
来运行,每一个逻辑处理器都分别绑定到单个操做系统线程上。这里涉及到三个角色:安全
每一个P会维护一个全局运行队列(称为runqueue),处于ready就绪状态的goroutine
(灰色G)被放在这个队列中等待被调度。在编写程序时,每当go func
启动一个goroutine
时,runqueue
便在尾部加入一个goroutine
。在下一个调度点上,P就从runqueue
中取出一个goroutine
出来执行(蓝色G)。并发
当某个操做系统线程M阻塞的时候(好比goroutine
执行了阻塞的系统调用),P能够绑定到另一个操做系统线程M上,让运行队列中的其余goroutine
继续执行:函数
上图中G0执行了阻塞操做,M0被阻塞,P将在新的系统线程M1上继续调度G执行。M1有多是被新建立的,或者是从线程缓存中取出。Go调度器保证有足够的线程来运行全部的P,语言运行时默认限制每一个程序最多建立10000个线程,这个如今能够经过调用runtime/debug包的SetMaxThreads
方法来更改。工具
Go能够在在一个逻辑处理器P上实现并发,若是须要并行,必须使用多于1个的逻辑处理器。Go调度器会把goroutine
平等分配到每一个逻辑处理器上,此时goroutine
将在不一样的线程上运行,不过前提是要求机器拥有多个物理处理器。ui
使用关键字go
来建立一个goroutine
,并让全部的goroutine
都获得执行:atom
//example1.go package main import ( "runtime" "sync" "fmt" ) var ( wg sync.WaitGroup ) func main() { //分配一个逻辑处理器P给调度器使用 runtime.GOMAXPROCS(1) //在这里,wg用于等待程序完成,计数器加2,表示要等待两个goroutine wg.Add(2) //声明1个匿名函数,并建立一个goroutine fmt.Printf("Begin Coroutines\n") go func() { //在函数退出时,wg计数器减1 defer wg.Done() //打印3次小写字母表 for count := 0; count < 3; count++ { for char := 'a'; char < 'a'+26; char++ { fmt.Printf("%c ", char) } } }() //声明1个匿名函数,并建立一个goroutine go func() { defer wg.Done() //打印大写字母表3次 for count := 0; count < 3; count++ { for char := 'A'; char < 'A'+26; char++ { fmt.Printf("%c ", char) } } }() fmt.Printf("Waiting To Finish\n") //等待2个goroutine执行完毕 wg.Wait() }
这个程序使用runtime.GOMAXPROCS(1)
来分配一个逻辑处理器给调度器使用,两个goroutine
将被该逻辑处理器调度并发执行。程序输出:操作系统
Begin Coroutines Waiting To Finish A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z
从输出来看,是先执行完一个goroutine
,再接着执行第二个goroutine
的,大写字母所有打印完后,再打印所有的小写字母。那么,有没有办法让两个goroutine
并行执行呢?为程序指定两个逻辑处理器便可:线程
//修改成2个逻辑处理器 runtime.GOMAXPROCS(2)
此时执行程序,输出为:
Begin Coroutines Waiting To Finish A B C D E a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z a b c F G H I J K L M N O P Q R S T U V W X d e f g h i j k l m n o p q r s Y Z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
那若是只有1个逻辑处理器,如何让两个goroutine交替被调度?实际上,若是goroutine
须要很长的时间才能运行完,调度器的内部算法会将当前运行的goroutine
让出,防止某个goroutine
长时间占用逻辑处理器。因为示例程序中两个goroutine
的执行时间都很短,在为引发调度器调度以前已经执行完。不过,程序也可使用runtime.Gosched()
来将当前在逻辑处理器上运行的goruntine
让出,让另外一个goruntine
获得执行:
//example2.go package main import ( "runtime" "sync" "fmt" ) var ( wg sync.WaitGroup ) func main() { //分配一个逻辑处理器P给调度器使用 runtime.GOMAXPROCS(1) //在这里,wg用于等待程序完成,计数器加2,表示要等待两个goroutine wg.Add(2) //声明1个匿名函数,并建立一个goroutine fmt.Printf("Begin Coroutines\n") go func() { //在函数退出时,wg计数器减1 defer wg.Done() //打印3次小写字母表 for count := 0; count < 3; count++ { for char := 'a'; char < 'a'+26; char++ { if char=='k'{ runtime.Gosched() } fmt.Printf("%c ", char) } } }() //声明1个匿名函数,并建立一个goroutine go func() { defer wg.Done() //打印大写字母表3次 for count := 0; count < 3; count++ { for char := 'A'; char < 'A'+26; char++ { if char == 'K'{ runtime.Gosched() } fmt.Printf("%c ", char) } } }() fmt.Printf("Waiting To Finish\n") //等待2个goroutine执行完毕 wg.Wait() }
两个goroutine
在循环的字符为k/K的时候会让出逻辑处理器,程序的输出结果为:
Begin Coroutines Waiting To Finish A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A B C D E F G H I J a b c d e f g h i j K L M N O P Q R S T U V W X Y Z A B C D E F G H I J k l m n o p q r s t u v w x y z a b c d e f g h i j K L M N O P Q R S T U V W X Y Z k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z
这里大小写字母果真是交替着输出了。不过从输出能够看到,第一次输出大写字母时遇到K没有让出逻辑处理器,这是什么缘由还不是很清楚,调度器的调度机制?
并发程序避免不了的一个问题是对资源的同步访问。若是多个goroutine
在没有互相同步的状况下去访问同一个资源,并进行读写操做,这时goroutine
就处于竞争状态下:
//example3.go package main import ( "sync" "runtime" "fmt" ) var ( //counter为访问的资源 counter int64 wg sync.WaitGroup ) func addCount() { defer wg.Done() for count := 0; count < 2; count++ { value := counter //当前goroutine从线程退出 runtime.Gosched() value++ counter=value } } func main() { wg.Add(2) go addCount() go addCount() wg.Wait() fmt.Printf("counter: %d\n",counter) } //output: counter: 4 或者counter: 2
这段程序中,goroutine
对counter
的读写操做没有进行同步,goroutine 1对counter的写结果可能被goroutine 2所覆盖。Go可经过以下方式来解决这个问题:
chan
有时候竞争状态并不能一眼就看出来。Go 提供了一个很是有用的工具,用于检测竞争状态。使用方式是:
go build -race example4.go//用竞争检测器标志来编译程序
./example4 //运行程序
工具检测出了程序存在一处竞争状态,并指出发生竞争状态的几行代码是:
22 counter=value 18 value := counter 28 go addCount() 29 go addCount()
对整形变量或指针的同步访问,可使用原子函数来进行。这里使用原子函数来修复example4.go中的竞争状态问题:
//example5.go package main import ( "sync" "runtime" "fmt" "sync/atomic" ) var ( //counter为访问的资源 counter int64 wg sync.WaitGroup ) func addCount() { defer wg.Done() for count := 0; count < 2; count++ { //使用原子操做来进行 atomic.AddInt64(&counter,1) //当前goroutine从线程退出 runtime.Gosched() } } func main() { wg.Add(2) go addCount() go addCount() wg.Wait() fmt.Printf("counter: %d\n",counter) } //output: counter: 4
这里使用atomic.AddInt64
函数来对一个整形数据进行加操做,另一些有用的原子操做还有:
atomic.StoreInt64() //写 atomic.LoadInt64() //读
更多的原子操做函数请看atomic
包中的声明。
对临界区的访问,可使用互斥锁来进行。对于example4.go的竞争状态,可使用互斥锁来解决:
//example5.go package main import ( "sync" "runtime" "fmt" ) var ( //counter为访问的资源 counter int wg sync.WaitGroup mutex sync.Mutex ) func addCount() { defer wg.Done() for count := 0; count < 2; count++ { //加上锁,进入临界区域 mutex.Lock() { value := counter //当前goroutine从线程退出 runtime.Gosched() value++ counter = value } //离开临界区,释放互斥锁 mutex.Unlock() } } func main() { wg.Add(2) go addCount() go addCount() wg.Wait() fmt.Printf("counter: %d\n", counter) } //output: counter: 4
使用Lock()
与Unlock()
函数调用来定义临界区,在同一个时刻内,只有一个goroutine可以进入临界区,直到调用Unlock()
函数后,其余的goroutine才可以进入临界区。
在Go中解决共享资源安全访问,更经常使用的使用通道chan。
Go语言采用CSP消息传递模型。经过在goroutine
之间传递数据来传递消息,而不是对数据进行加锁来实现同步访问。这里就须要用到通道chan
这种特殊的数据类型。当一个资源须要在goroutine
中共享时,chan在goroutine
中间架起了一个通道。通道使用make
来建立:
unbuffered := make(char int) //建立无缓存通道,用于int类型数据共享 buffered := make(chan string,10)//建立有缓存通道,用于string类型数据共享 buffered<- "hello world" //向通道中写入数据 value:= <-buffered //从通道buffered中接受数据
通道用于放置某一种类型的数据。建立通道时指定通道的大小,将建立有缓存的通道。无缓存通道是一种同步通讯机制,它要求发送goroutine
和接收goroutine
都应该准备好,不然会进入阻塞。
无缓存通道是同步的——一个goroutine
向channel写入消息的操做会一直阻塞,直到另外一个goroutine
从通道中读取消息。反过来也是,一个goroutine
从channel读取消息的操做会一直阻塞,直到另外一个goroutine
向通道中写入消息。《Go in action》中关于无缓存通道的解释有一个很是棒的例子:网球比赛。在网球比赛中,两位选手老是处在如下两种状态之一:要么在等待接球,要么在把球打向对方。球的传递可看为通道中数据传递。下面这段代码使用通道模拟了这个过程:
//example6.go package main import ( "sync" "fmt" "math/rand" "time" ) var wg sync.WaitGroup func player(name string, court chan int) { defer wg.Done() for { //若是通道关闭,那么选手胜利 ball, ok := <-court if !ok { fmt.Printf("Player %s Won\n", name) return } n := rand.Intn(100) //随机几率使某个选手Miss if n%13 == 0 { fmt.Printf("Player %s Missed\n", name) //关闭通道 close(court) return } fmt.Printf("Player %s Hit %d\n", name, ball) ball++ //不然选手进行击球 court <- ball } } func main() { rand.Seed(time.Now().Unix()) court := make(chan int) //等待两个goroutine都执行完 wg.Add(2) //选手1等待接球 go player("candy", court) //选手2等待接球 go player("luffic", court) //球进入球场(能够开始比赛了) court <- 1 wg.Wait() } //output: Player luffic Hit 1 Player candy Hit 2 Player luffic Hit 3 Player candy Hit 4 Player luffic Hit 5 Player candy Missed Player luffic Won
有缓存的通道是一种在被接收前能存储一个或者多个值的通道,它与无缓存通道的区别在于:无缓存的通道保证进行发送和接收的goroutine会在同一时间进行数据交换,有缓存的通道没有这种保证。有缓存通道让goroutine
阻塞的条件为:通道中没有数据可读的时候,接收动做会被阻塞;通道中没有区域容纳更多数据时,发送动做阻塞。向已经关闭的通道中发送数据,会引起panic,可是goroutine
依旧能从通道中接收数据,可是不能再向通道里发送数据。因此,发送端应该负责把通道关闭,而不是由接收端来关闭通道。
(完)