go的happens-before原则和内存可见性

提示:目录自动生成


前言

提示:本文要记录的大概内容:
golang的happens-before原则,前面一个操作的结果对后续操作是可见的,在保证了这一原则下就能保证内存可见性的正确性,


提示:以下是本篇文章正文内容

一、happens-before原则

1. Initialization

If a package p imports package q, the completion of q’s init functions happens before the start of any of p’s.

2. Goroutine creation

The go statement that starts a new goroutine happens before the goroutine’s execution begins.

3. Goroutine destruction

The exit of a goroutine is not guaranteed to happen before any event in the program.

4. Channel communiaction

4.1

A send on a channel happens before the corresponding receive from that channel completes.

4.2

The closing of a channel happens before a receive that returns a zero value because the channel is closed.

4.3

A receive from an unbuffered channel happens before the send on that channel completes.

4.4

The kth receive on a channel with capacity C happens before the k+Cth send from that channel completes.

5. Locks

5.1

For any sync.Mutex or sync.RWMutex variable l and n < m, call n of l.Unlock() happens before call m of l.Lock() returns.

5.2

For any call to l.RLock on a sync.RWMutex variable l, there is an n such that the l.RLock happens (returns) after call n to l.Unlock and the matching l.RUnlock happens before call n+1 to l.Lock.

6. Once

A single call of f() from once.Do(f) happens (returns) before any call of once.Do(f) returns.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值