NOTE
2.13 concurrent
1. 有什么 1.1. goroutine+channel - goroutine.md - channel.md 1.2. sync包 sync.md 2. 同步 2.1. 顺序一致性模型 同一个Goroutine线程内部,顺序一致性内存模型是得到保证的 2.2. happens-before 不
这是历史学习笔记,可能存在过时或不完整的理解。
1. 有什么
1.1. goroutine+channel
1.2. sync包
2. 同步
2.1. 顺序一致性模型
同一个Goroutine线程内部,顺序一致性内存模型是得到保证的
2.2. happens-before
不同的Goroutine之间,并不满足顺序一致性内存模型,需要通过明确定义的同步事件来作为同步的参考
2.2.1. channel
- buffered channel:
ch <- v≤v <- ch- 解释:发送数据到buffered channel **happens before **从buffered channel中接收数据
- 即需要writer先生产数据,丢入channel中;reader才能从channel中消费数据
- unbuffered channel:
v <- ch≤ch <- v- 解释:从unbuffered channel中接收数据 happens before 发送数据到unbuffered channel
- 即reader从channel中读取数据,会一直阻塞到writer往channel生产数据
2026 注:这里是简化记法。更精确地说,send happens before 对应 receive 完成;无缓冲 channel 中 receive happens before 对应 send 完成。
3. 并发模式
4. 多核CPU
Go语言多核并行化 PythonWise: CPU Affinity in Go tsingson/cpuaffinity: pin goroutine to cpu core as thread