首页 > 其他 > 详细

use a synchronization mechanism such as a lock or channel communication to establish a relative ordering.

时间:2020-01-19 22:31:31      阅读:87      评论:0      收藏:0      [点我收藏+]

https://golang.org/ref/mem#tmp_6

 

Goroutine destruction

The exit of a goroutine is not guaranteed to happen before any event in the program. For example, in this program:

var a string

func hello() {
	go func() { a = "hello" }()
	print(a)
}

the assignment to a is not followed by any synchronization event, so it is not guaranteed to be observed by any other goroutine. In fact, an aggressive compiler might delete the entire go statement.

If the effects of a goroutine must be observed by another goroutine, use a synchronization mechanism such as a lock or channel communication to establish a relative ordering.

Channel communication

Channel communication is the main method of synchronization between goroutines. Each send on a particular channel is matched to a corresponding receive from that channel, usually in a different goroutine.

use a synchronization mechanism such as a lock or channel communication to establish a relative ordering.

原文:https://www.cnblogs.com/yuanjiangw/p/12209743.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!