前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang语言--Coroutine可能存在的死锁

Golang语言--Coroutine可能存在的死锁

作者头像
李海彬
发布2018-03-21 11:33:50
8400
发布2018-03-21 11:33:50
举报
文章被收录于专栏:Golang语言社区Golang语言社区

直接上代码:

1. 第一种情况, 如果没有select{}, main 主线程不会等待coroutine运行,导致coroutine得不到机会运行。

You are requesting eventual scheduling (using the two go statements) of two goroutines and then you exit main without giving the scheduler a chance to do anything.

有了select, 程序正常运行。

2. coroutine有机会运行,但是会发生死锁, fatal error: all goroutines are asleep - deadlock!

The goroutine executing func1 exited, ditto for func2. The main goroutine is blocked with no hope to recover while no other goroutine can be scheduled.

3. 第三种情况, 正常运行。

4. 实现上面的目的的另外一种方法:

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-05-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Golang语言社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档