当前位置:网站首页>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
边栏推荐
- 10_那些格調很高的個性簽名
- 不要小看了积分商城,它的作用可以很大
- Zhiyuan community weekly 86: Gary Marcus talks about three linguistic factors that can be used for reference in large model research; Google puts forward the Wensheng graph model parti which is compar
- Online text entity extraction capability helps applications analyze massive text data
- Rasa 3. X learning series - it is a great honor to be a source code contributor of Rasa contributors, and to build and share the rasa community with rasa source code contributors all over the world!
- 【Pytorch】量化
- MES在流程和离散制造企业的15个差别(下)
- Telecommuting: camping at home office gadgets | community essay solicitation
- leetcode:1504. Count the number of all 1 sub rectangles
- [deep learning] storage form of nchw, nhwc and chwn format data
猜你喜欢

AntD checkbox,限制选中数量

【无标题】

打败 二叉树!

P2PDB 白皮书
![Generate binary tree according to preorder & inorder traversal [partition / generation / splicing of left subtree | root | right subtree]](/img/f7/8d026c0e4435fc8fd7a63616b4554d.png)
Generate binary tree according to preorder & inorder traversal [partition / generation / splicing of left subtree | root | right subtree]

A review of text contrastive learning

box-sizing

The "little giant" specialized in special new products is restarted, and the "enterprise cloud" digital empowerment
![[deep learning] storage form of nchw, nhwc and chwn format data](/img/4f/4478d96132eb2547f6ec09ae49639e.jpg)
[deep learning] storage form of nchw, nhwc and chwn format data

puzzle(016.2)指画星河
随机推荐
Kotlin shared mutable state and concurrency
postgresql 之 ilist
Defoaming
Research on MySQL composite index
Puzzle (016.2) finger painting Galaxy
win10系统问题
SSH keygen configuration does not require entering a password every time
NPM package [details] (including NPM package development, release, installation, update, search, uninstall, view, version number update rules, package.json details, etc.)
Grendao usage problems
AQS初探
融云通信“三板斧”,“砍”到了银行的心坎上
Jerry's test mic energy automatic recording automatic playback reference [article]
Common sense knowledge points
Idea connection MySQL custom generated entity class code
A review of text contrastive learning
Digital business cloud: strengthen supplier management and promote efficient collaboration between air transport enterprises and suppliers
MySQL log management, backup and recovery
MES在流程和离散制造企业的15个差别(下)
leetcode:1504. 统计全 1 子矩形的个数
Jerrys timer0 uses the default pa13 to detect the pulse width [chapter]