为了账号安全,请及时绑定邮箱和手机立即绑定

runtime.Gosched到底做什么?

runtime.Gosched到底做什么?

Go
狐的传说 2021-05-08 14:02:22
在Tour of Go网站的go 1.5发行之前的版本中,有一段代码看起来像这样。package mainimport (    "fmt"    "runtime")func say(s string) {    for i := 0; i < 5; i++ {        runtime.Gosched()        fmt.Println(s)    }}func main() {    go say("world")    say("hello")}输出看起来像这样:helloworldhelloworldhelloworldhelloworldhello令我困扰的是,runtime.Gosched()将其删除后,该程序不再显示“世界”。hellohellohellohellohello为什么会这样?如何runtime.Gosched()影响执行力?
查看完整描述

2 回答

?
喵喵时光机

TA贡献1846条经验 获得超7个赞

合作计划是罪魁祸首。如果不屈服,则另一个(例如“世界”)goroutine可能合法地在main终止之前/之时获得零执行机会,根据规范,终止所有goroutine的时间即为- 整个过程。


查看完整回答
反对 回复 2021-05-17
  • 2 回答
  • 0 关注
  • 202 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号