1. 首页
  2. 主题
  3. Go问与答

关于并发的问题,如下代码该如何理解?

yuchen16 · · 1432 次点击
### The Go Memory Model 文章结尾有这么一段代码,理解的不是很透彻,谁给讲解一下,谢谢! There are subtler variants on this theme, such as this program. ``` type T struct { msg string } var g *T func setup() { t := new(T) t.msg = "hello, world" g = t } func main() { go setup() for g == nil { } print(g.msg) } ``` Even if main observes g != nil and exits its loop, there is no guarantee that it will observe the initialized value for g.msg. In all these examples, the solution is the same: use explicit synchronization.
90design
狗狼都是神
实际上执行 go setup()时, go程并没有执行完,然后继续往下执行 for循环(只要g == nil一直循环下去), 直到go程返回结束,此时g != nil
#6
更多评论
不能保证 setup 先执行
#1
意思是不能保证setup先返回?
#2

用户登录

没有账号?注册

今日阅读排行

    加载中

一周阅读排行

    加载中