文章目录
前言
今天写代码的时候突发奇想goroutine中是否可以再运行一个goroutine,初步的想法是可以的,因为main函数运行的时候其实是一个主goroutine,在主goroutine里面再运行一个goroutine是没问题的,那么我们一个普通的goroutine运行一个goroutine不也是同理?
代码
我们直接上代码
package main
import (
"fmt"
"time"
)
func main() {
go func() {
go func() {
time.Sleep(time.Duration(3) * time.Second)
fmt.Println("hey")
}()
time.Sleep(time.Duration(7) * time.Second)
}()
//time.Sleep(time.Duration(8) * time.Second)
}
发现上面代码运行什么也没打印直接退出,想了半天是主goroutine创建完goroutine就退出,导致goroutine还没执行子goroutine程序就结束了,所以把上述代码的注释去掉
package main
import (
"fmt"
"time"
)
func main() {
go func() {
go func() {
time.Sleep(time.Duration(3) * time.Second)
fmt.Println("hey")
}()
time.Sleep(time.Duration(7) * time.Second)
}()
time.Sleep(time.Duration(8) * time.Second)
}
下面会提及第一层goroutine(主goroutine),第二层goroutine(第一个go func(){}()),第三层goroutine(最里面的go func (){}())
再运行果真打印了hey,这个时候换个玩法,讲中间的goroutine sleep时间改为3,第三层goroutine的sleep时间改为7,按照我们上面的推测应该不会打印,因为第二层go func在创建第三层goroutine后sleep 3秒就退出,第三层goroutine需要sleep7秒才能打印,所以按照常理第三层goroutine应该还在sleep的时候创建它的goroutine(第二层goroutine)就已经凉了,所以他也不会打印,看下面代码
package main
import (
"fmt"
"time"
)
func main() {
go func() {
go func() {
time.Sleep(time.Duration(7) * time.Second)
fmt.Println("hey")
}()
time.Sleep(time.Duration(3) * time.Second)
}()
time.Sleep(time.Duration(8) * time.Second)
}
运行完后发现还是打印了hey…这是啥情况
goroutine原理
为了解决这个问题我们有必要看一下goroutine的原理
先对比一下goroutine和线程
goroutine | thread
___________________________________________________________________________________________
Goroutines are managed by the go runtime. | Operating system threads are managed by kernal.
___________________________________________________________________________________________
Goroutine are not hardware dependent. | Threads are hardware dependent.
___________________________________________________________________________________________
Goroutines have easy communication medium | Thread does not have easy communication
known as channel. | medium.
___________________________________________________________________________________________
Due to the presence of channel one | Due to lack of easy communication medium
goroutine can communicate with other | inter-threads communicate takes place with
goroutine with low latency. | high latency.
___________________________________________________________________________________________
Goroutine does not have ID because | Threads have their own unique ID because
go does not have Thread Local Storage | they have Thread Local Storage.
___________________________________________________________________________________________
Goroutines are cheaper than threads. | The cost of threads are higher than goroutine.
___________________________________________________________________________________________
They are cooperatively scheduled. | They are preemptively scheduled.
___________________________________________________________________________________________
They have fasted startup time than threads. | They have slow startup time than goroutines.
___________________________________________________________________________________________
Goroutine has growable segmented stacks. | Threads does not have growable segmented stacks.
在此之前可以先了解一下user-level thread vs kernel level thread 1
顺带说一句linux中pthreads库关于线程的实现是用的clone
,也就是说pthread_create(...)
创建的是一个kernel线程
以下参考Detailed Go routine scheduling2
根据上述的链接所解释,我们的goroutine是Green thread
,由语言实现,我们把操作系统看成一个软件,我们可以发现这个软件有非常多的线程来干一些非常底层的事情,比如直接操作硬件等等,我们把goroutine看成user space的线程,把操作系统自带的线程看成kernel space的线程,一个goroutine最终可能在一个或者多个kernel thread上运行,一个kernel thread也可能对应多个 green thread,因为一个kernel thread都是对应一个功能
go使用者不能创建kernel thread!!!但是go的runtime确可以 linux下c可以通过pthreads库创建
go在开始程序的时候会干什么
先说几个关于goroutine的概念
processor:这个processor不是cpu里面的核,而是go里面的概念,它用于执行goroutine,维护一个goroutine的列表,processor可以从属一个kernel thread,假设一个processor从属了一个kernel thread,那么其上面的goroutine都在运行,假设processor不从属一个kernel thread,那么其上面的goroutine都需要等待调度,GOMAXPROCS
就是设置processor的变量,假如设置大了就有可能发生其上面的goroutine需要等待调度的情况
首先go会初始化Scheduler,
其次我们的主goroutine创建一个kernel thread(主goroutine和其他的goroutine不一样),这个主goroutine有一个作用就是监控其他goroutine,这个主goroutine在main函数执行的时候生成,
创建一个普通goroutine会发生什么
首先当我们创建一个普通的goroutine我们先指定一个代码片段给goroutine,可以是函数,可以是匿名函数,然后这个普通的goroutine会被添加到processor上,然后这个新的goroutine会包含stack address,PC(program counter)
goroutine的状态
goroutine的状态和关系如下图
解释一下各个状态
Gilde:字面意思,创建的goroutine什么资源也没有分配
Grunnable:在分配并且初始化资源后新的gouroutine进入这个状态,且这个goroutine已经关联Processor,并且processor在某个kernel thread之上
Grunning:running是个什么状态呢,当这个goroutine等待到了cpu的空闲,他将进入Grunning状态,
Gwait:和传统的wait一样goroutine执行了某个阻塞操作就wait,比如channel操作,IO操作等等,当ready后就便会grunable状态了,等到cpu有空闲再变成grunning状态
Gsyscall:顾名思义,当goroutine进行syscall的时候变成gsyscall状态,等结束返回成grunable 状态,等待cpu有空闲变回Grunning状态
回到问题
有了以上的基础我们回到之前的问题上来,为什么第三层goroutine在第二层goroutine结束后还会继续运行?
TODO