当前位置:网站首页>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
边栏推荐
- web技术分享| 【地图】实现自定义的轨迹回放
- well! Do you want to have a romantic date with the shining "China Star"?
- Psexec right raising
- What is pseudo static? How to configure the pseudo static server?
- TCP three handshakes and four waves
- web rdp Myrtille
- 开源之夏2022中选结果公示,449名高校生将投入开源项目贡献
- How to remote server is the price of the server expensive
- [Mid Autumn Festival greeting new year with good gifts] Tengyun pioneer feedback exchange group received new benefits!
- The practice of tidb slow log in accompanying fish
猜你喜欢
随机推荐
Student information management system user manual
618 promotion: mobile phone brand "immortal fight", high-end market "who dominates the ups and downs"?
ModStartCMS 企业内容建站系统(支持 Laravel9)v4.2.0
讲讲我的不丰富的远程办公经验和推荐一些办公利器 | 社区征文
Old popup explorer Exe has stopped working due to problems. What should I do?
The collection method of penetration test, and which methods can be used to find the real IP
Web penetration test - 5. Brute force cracking vulnerability - (1) SSH password cracking
How to be a web server and what are the advantages of a web server
Hprof information in koom shark with memory leak
On game safety (I)
内存泄漏之KOOM-Shark中的Hprof信息
2021 graphic design trend: aesthetic response to chaos
Build a small program + management background in 7 days, and this goose factory HR is blessed!
How should the server be placed?
hprofStringCache
Black hat actual combat SEO: never be found hijacking
Several good books for learning data
Web penetration test - 5. Brute force cracking vulnerability - (2) SNMP password cracking
Can the video streams of devices connected to easygbs from the intranet and the public network go through their respective networks?
hprofStringCache



![[Numpy] Numpy对于NaN值的判断](/img/aa/dc75a86bbb9f5a235b1baf5f3495ff.png)




![[code Capriccio - dynamic planning] t392 Judgement subsequence](/img/59/9da6d70195ce64b70ada8687a07488.png)
