当前位置:网站首页>Go language Chanel memory model
Go language Chanel memory model
2022-06-24 04:04:00 【Wangxiaoming_ HIT】
Memory model
Go The memory model describes “ In a groutine Reading variables in can detect in other gorountine Write operation of change amount in ” Conditions .
happen-before Definition
To specify the requirements of reads and writes, we define happens before, a partial order on the execution of memory operations in a Go program. If event e1 happens before event e2, then we say that e2 happens after e1. Also, if e1 does not happen before e2 and does not happen after e2, then we say that e1 and e2 happen concurrently.
This is a Happens Before The definition of , If e1 It happened in e2 Before , So let's say e2 It happened in e1 after , If e1 Neither in e2 front , Not at all e2 after , Let's say the two are concurrent .
About channel Of happens-before stay Go There are three cases mentioned in the memory model :
- case1: To a channel Send operation of happens-before The corresponding channel The receive operation for is completed
- case2: Close a channel happens-before From the Channel Received the last return value 0
- case3: Unbuffered channel Receiving operation of happens-before The corresponding channel Before the send operation
case1: To a channel Send operation of happens-before The corresponding channel The receive operation for is completed
Test code :
import "testing"
var c = make(chan int, 10)
var a string
func f() {
a = "hello, world" // (1)
c <- 0 // (2) Write operations Sending operation
}
func TestMemoryModel(t *testing.T) {
go f()
<-c // (3) // Receive operation
print(a) // (4)
} The above code , Will be guaranteed to print out hello world . There's a cushion channel The write operation occurs before the receive operation .
Unbuffered channel Receiving operation of happens-before The corresponding channel Before the send operation
var c1 = make(chan int)
var a1 string
func f1() {
a1 = "hello, world" // (1)
<-c1 // (2) Receive operation
}
func TestMemoryModel1(t *testing.T) {
go f1()
c1 <- 0 // (3) Sending operation
print(a1) // (4)
}Running results :
=== RUN TestMemoryModel1 hello, world--- PASS: TestMemoryModel1 (0.00s) PASS
The above code will ensure that it will print hello world . because
According to the third rule above (2) happens-before (3), Finally, we can guarantee (1) happens-before (4).
No buffer channel The receive operation occurs before the write operation .
Let's take another example
var c2= make(chan int, 1)
var a2 string
func f2() {
a2 = "hello, world" // (1)
<-c2 // Receive operation
}
// There is no guarantee Print out "hello, world"
func TestMemoryModel2(t *testing.T) {
go f2()
c2 <- 0 // (3)
print(a2) // (4) Write operations
//var day time.Time
//print(day.Format("20060102"))
} The above code is not guaranteed to print hello world , Because of the output channel It's buffered , There is no guarantee that the receive operation occurs before the write operation , However, it can ensure that the write operation occurs before the receive operation .
Welcome to the official account : The path of programmer wealth and freedom
Reference material
- https://golang.org/ref/mem
- https://lailin.xyz/post/go-training-week3-go-memory-model.html
- https://www.jdon.com/concurrent/golang-memory.html
边栏推荐
- Koom of memory leak
- What is a 1U server? What industries can 1U servers be used in?
- API real-time signature scheme based on Yapi
- 系统的去学习一门编程语言,原来有如此捷径
- Understanding of structure in C language
- Flutter series: offstage in flutter
- Rasa 3.x 学习系列-Rasa 3.2.0 新版本发布
- The practice of tidb slow log in accompanying fish
- 祝贺钟君成为 CHAOSS Metric Model 工作组的 Maintainer
- What is FTP? What is the FTP address of the ECS?
猜你喜欢

Black hat SEO actual combat directory wheel chain generates millions of pages in batch

Modstartcms theme introductory development tutorial

Clickhouse (02) Clickhouse architecture design introduction overview and Clickhouse data slicing design

ModStartCMS 企业内容建站系统(支持 Laravel9)v4.2.0

Brief ideas and simple cases of JVM tuning - how to tune

SQL注入绕过安全狗思路一

应用实践 | Apache Doris 整合 Iceberg + Flink CDC 构建实时湖仓一体的联邦查询分析架构

Clang代码覆盖率检测(插桩技术)

Black hat SEO practice: General 301 weight PR hijacking
![[Numpy] Numpy对于NaN值的判断](/img/aa/dc75a86bbb9f5a235b1baf5f3495ff.png)
[Numpy] Numpy对于NaN值的判断
随机推荐
What should I pay attention to when choosing a data center?
MySQL cases SQL causes 100% CPU utilization
After 20 years of development, is im still standing still?
Cloud development CMS Enterprise Edition demand survey
系统的去学习一门编程语言,原来有如此捷径
应用实践 | Apache Doris 整合 Iceberg + Flink CDC 构建实时湖仓一体的联邦查询分析架构
Installation of pytorch in pycharm
Do you understand TLS protocol?
2021 graphic design trend: aesthetic response to chaos
Black hat SEO actual combat directory wheel chain generates millions of pages in batch
Structure size calculation of C language struct
hprofStringCache
ModStartCMS 企业内容建站系统(支持 Laravel9)v4.2.0
黑帽实战SEO之永不被发现的劫持
Protect your system with fail2ban and firewalld blacklists
openGauss 3.0版本源码编译安装指南
Why is on-line monitoring of equipment more and more valued by people?
How to monitor multiple platforms simultaneously when easydss/easygbs platform runs real-time monitoring?
The collection method of penetration test, and which methods can be used to find the real IP
Browser rendering mechanism