硬件同步原语其实对应的是 Go 语言 atomic 包下的一些原子操作
package main
import (
"fmt"
"sync"
"sync/atomic"
)
func main() {
// 账户初始值为0元
var balance int32
balance = int32(0)
done := make(chan bool)
// 执行10000次转账,每次转入1元
count := 10000
//var lock sync.Mutex
for i := 0; i < count; i++ {
// 这里模拟异步并发转账
//go transfer(&balance, 1, done, &lock)
//go transferCas(&balance, 1, done)
go transferFaa(&balance, 1, done)
}
// 等待所有转账都完成
for i := 0; i < count; i++ {
<-done
}
// 打印账户余额
fmt.Printf("balance = %d \n", balance)
}
// 转账服务
func transfer(balance *int32, amount int, done chan bool, lock *sync.Mutex) {
lock.Lock()
*balance = *balance + int32(amount)
lock.Unlock()
done <- true
}
// CAS原语
func transferCas(balance *int32, amount int, done chan bool) {
for {
oldValue := atomic.LoadInt32(balance)
newValue := oldValue + int32(amount)
if atomic.CompareAndSwapInt32(balance, oldValue, newValue) {
break
}
}
done <- true
}
// FAA原语
func transferFaa(balance *int32, amount int, done chan bool) {
atomic.AddInt32(balance, int32(amount))
done <- true
}
网友评论