如何实现主线程等待goroutine完成?
时间:2024-02-19
浏览:396
作者:佚名
Golang小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《如何实现主线程等待goroutine完成?》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!
问题内容
我试图了解 go 关键字在调用方法时的真正作用。据我了解,在方法之前放置 go 关键字将触发 go 在 goroutine 上执行此函数,该 goroutine 可以位于同一处理器或不同处理器上。
但是当我练习以下代码时,我对没有返回预期输出的情况感到困惑,这些输出是“从 $some-time-stamp 开始”和“获取消息:$some-time-stamp”。
更具体的问题描述在代码注释中。谢谢。
package main
import (
"fmt"
"time"
)
func main() {
message := make(chan string, 1)
// I am aware of if I make a channel this way, it's asynchronous non-blocking
workOnWriteOnlyChannel(message)
workOnReadOnlyChannel(message)
/*
start at 2009-11-10 23:00:05 +0000 UTC m=+5.000000001
get message : done at 2009-11-10 23:00:05 +0000 UTC m=+5.000000001
*/
// the above one worked as expected, is because it runs synchronously, no blocking is happening
go workOnWriteOnlyChannel(message)
workOnReadOnlyChannel(message)
/*
start at 2009-11-10 23:00:05 +0000 UTC m=+5.000000001
get message : done at 2009-11-10 23:00:05 +0000 UTC m=+5.000000001
*/
// main thread executes to create a goroutine(let's call it goroutine-2) which is responsible to execute method
// workOnWriteOnlyChannel, then the main thread goes to the next line immediately
// main thread executes method workOnReadOnlyChannel, and main thread will block for 5 seconds because goroutine-2 won't update the shared channel until 5 seconds after
// so after 5 seconds, the main thread will unblock
workOnWriteOnlyChannel(message)
go workOnReadOnlyChannel(message)
/*
start at 2009-11-10 23:00:05 +0000 UTC m=+5.000000001
*/
// I thought after calling workOnWriteOnlyChannel, the shared channel has value, though the
// method workOnReadOnlyChannel is executed by another goroutine, it should be two outputs,
// but it didn't which confuses me
go workOnReadOnlyChannel(message)
workOnWriteOnlyChannel(message)
/*
start at 2009-11-10 23:00:05 +0000 UTC m=+5.000000001
*/
// I thought a new goroutine(let's call it goroutine-1) will execute method
// workOnReadOnlyChannel, but at first 5 seconds, it will be blocked,
// but the main thread is not blocked, and when the main thread goes to execute method
// workOnWriteOnlyChannel, the channel will have one element, and goroutine-1 will unblock,
// but it didn't which confuses me
/*
workOnReadOnlyChannel(message)
go workOnWriteOnlyChannel(message)
*/
// fatal error: all goroutines are asleep - deadlock!
// I think I know this one, correct me if I'm wrong
// main thread will block when it goes to execute method workOnReadOnlyChannel,
// so can't go to the next line,
// so deadlock happens
}
func workOnWriteOnlyChannel(messages chan<- string) {
time.Sleep(5 * time.Second)
messages<- "done"
defer fmt.Printf("start at %s\n", time.Now())
}
func workOnReadOnlyChannel(messages <-chan string) {
fmt.Printf("get message : %s at %s\n", <-messages, time.Now())
}解决方案
我只是将适用的评论放在一起,以便我可以结束问题。 (说实话,我觉得我之前的问题标题真的很糟糕,没有传达出我真正问题的意思,改成现在的,感觉还是不太对劲,但是好多了。我觉得这个标题可以以后遇到同样困惑的人。)
主线程不等待 goroutine 完成。这是我最初困惑的答案。
至于如何让主线程等待goroutine:
方案一,强制主线程休眠一会儿,让goroutine在主线程结束之前返回;
方案二,使用sync.WaitGroup等待goroutines完成(用法与CountDownLatch类似)
以上就是《如何实现主线程等待goroutine完成?》的详细内容,更多关于的资料请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.27
11.27
11.27
11.27
热门专题
