[日常] Go语言圣经-竞争条件习题
package main
import(
"fmt"
"sync"
)
var balance int
func Deposit(amount int) { balance = balance + amount }
func Balance() int { return balance }
/*
问题:
1.在Alice运行期间 balance = balance + amount 这一步运算可能会被Bob中间挤占
2.当运行到balance + amount的时候,Bob的正好赶到,然后继续运行blance=
3.此时Bob的增加的数据会丢失
*/
func main(){
var wg sync.WaitGroup
wg.Add(1)
// Alice:
go func() {
defer wg.Done()
Deposit(200) // A1
fmt.Println("=", Balance()) // A2
}()
wg.Add(1)
// Bob:
go func(){
defer wg.Done()
Deposit(100)
}()
wg.Wait()
res:=Balance()
fmt.Println(res)
}
练习 9.1: 给gopl.io/ch9/bank1程序添加一个Withdraw(amount int)取款函数。其返回结果应该要表明事务是成功了还是因为没有足够资金失败了。这条消息会被发送给monitor的goroutine,且消息需要包含取款的额度和一个新的channel,这个新channel会被monitor goroutine来把boolean结果发回给Withdraw。
package main
import(
"fmt"
"sync"
)
var balance int
var deposits = make(chan int) //存款用channel
var balances = make(chan int) //接收余额用channel
func Deposit(amount int) {deposits <- amount}
func Balance() int { return <-balances }
func main(){
go teller()
var wg sync.WaitGroup
wg.Add(1)
go func(){
defer wg.Done()
Deposit(100)
fmt.Println("=",Balance())
}()
wg.Add(1)
go func(){
defer wg.Done()
Deposit(200)
fmt.Println("=",Balance())
}()
wg.Add(1)
go func(){
defer wg.Done()
res:=Withdraw(200)
if !res{
fmt.Println("取款失败")
}
}()
wg.Wait()
b:=Balance()
fmt.Println(b)
}
/*
解决:
1.总余额限定在一个goroutine中,通过channel通讯
2.channel是会阻塞同一时间的多个goroutine的
*/
func teller() {
var balance int //总余额限定在一个goroutine中
for {
select {
case amount := <-deposits:
balance += amount
case balances <- balance:
}
}
}
//取款用函数
func Withdraw(amount int)bool{
Deposit(-amount)
if Balance() < 0 {
Deposit(amount)
return false // insufficient funds
}
return true
}
十年开发经验程序员,离职全心创业中,历时三年开发出的产品《唯一客服系统》
一款基于Golang+Vue开发的在线客服系统,软件著作权编号:2021SR1462600。一套可私有化部署的网站在线客服系统,编译后的二进制文件可直接使用无需搭开发环境,下载zip解压即可,仅依赖MySQL数据库,是一个开箱即用的全渠道在线客服系统,致力于帮助广大开发者/公司快速部署整合私有化客服功能。
开源地址:唯一客服(开源学习版)
官网地址:唯一客服官网
浙公网安备 33010602011771号