当前位置:网站首页>Go language - use of goroutine coroutine
Go language - use of goroutine coroutine
2022-06-24 14:21:00 【Crying while learning】
Preface
Synergetic English coroutine,go The co process in language is goroutine.Go It's a concurrent language , Not parallel languages .
go The creation of language collaboration is very simple , Just add... To the front of the called function go keyword , The program will run a new goroutine.
however goroutine There is no return value , The return value is ignored . Therefore, it is often necessary to pass channel Conduct goroutine Communication between .
goroutine Use
func main() {
go mytest()
for i := 0; i < 100; i++ {
fmt.Println(" The main function outputs :", i)
}
}
func mytest() {
for i := 0; i < 500; i++ {
fmt.Println("goroutine Output numbers in :", i)
}
}
Output through this simple example , Can sum up goroutine Two characteristics of :
- goroutine The execution of the function in the coroutine and the execution of the main function are simultaneous , The output is alternating .
- After the main program , Even son goroutine Not done , It will also end with the main program .
sync package -WaitGroup
In general , After the main program , Son goroutine Will be over .
In order to make the main program wait for the sub groutine Execution completed , One way is to use sleep. however sleep There is a problem , We have no way to know goroutine The specific length of time to execute , Only try to lengthen it sleep Time for .
So we can use sync Bag WaitGroup Solve the problem that the main process waits for children goroutine The problem of .
var wg sync.WaitGroup
func main() {
wg.Add(1) // Definition WaitGroup The queue length , The length is 1
go mytest()
for i := 0; i < 100; i++ {
fmt.Println(" The main function outputs :", i)
}
wg.Wait() // At the end of the main program , wait for WaitGroup The queue is 0 And then exit
}
func mytest() {
for i := 0; i < 500; i++ {
fmt.Println("goroutine Output numbers in :", i)
}
wg.Done() // amount to wg.Add(-1), One goroutine end , call wg.Done(), Queue minus one
}
wg.Add(1) Main function , Definition WaitGroup The queue length
wg.Done() Son goroutine in , The function finally calls , Is equivalent to WaitGroup The queue length -1
wg.Wait() Main function , At the end of the main function , wait for WaitGroup The queue length is 0
边栏推荐
猜你喜欢

数字臧品系统开发 NFT数字臧品系统异常处理源码分享

Telecommuting: camping at home office gadgets | community essay solicitation

GO语言并发模型-MPG模型

Common sense knowledge points

A review of text contrastive learning

How to avoid placing duplicate orders
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]
![Maximum path sum in binary tree [handle any subtree, then handle the whole tree]](/img/d0/91ab1cc1851d7137a1cab3cf458302.png)
Maximum path sum in binary tree [handle any subtree, then handle the whole tree]

常见的单例模式&简单工厂

Mit-6.824-lab4a-2022 (ten thousand words explanation - code construction)
随机推荐
MySQL log management, backup and recovery
Jupiter notebook operation
Digital business cloud: strengthen supplier management and promote efficient collaboration between air transport enterprises and suppliers
IDEA连接mysql自定义生成实体类代码
How to solve the problem that iterative semi supervised training is difficult to implement in ASR training? RTC dev Meetup
Linux 安装 CenOS7 MySQL - 8.0.26
Online text entity extraction capability helps applications analyze massive text data
GO语言并发模型-MPG模型
conda和pip命令
二叉树中最大路径和[处理好任意一颗子树,就处理好了整个树]
Getting to know cloud native security for the first time: the best guarantee in the cloud Era
4 reasons for "safe left shift"
遠程辦公之:在家露營辦公小工具| 社區征文
Jericho turns on shouting in all modes to increase mic automatic mute [chapter]
Kunpeng arm server compilation and installation paddlepaddle
GO语言-init()函数-包初始化
Development of digital Tibetan product system NFT digital Tibetan product system exception handling source code sharing
Kotlin shared mutable state and concurrency
Virtual machines on the same distributed port group but different hosts cannot communicate with each other
[environment setup] zip volume compression