mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-11-01 10:41:35 +00:00
ch9: fix code format
This commit is contained in:
102
ch9/ch9-02.md
102
ch9/ch9-02.md
@@ -2,49 +2,49 @@
|
||||
|
||||
在8.6節中,我們使用了一個buffered channel作爲一個計數信號量,來保證最多隻有20個goroutine會同時執行HTTP請求。同理,我們可以用一個容量隻有1的channel來保證最多隻有一個goroutine在同一時刻訪問一個共享變量。一個隻能爲1和0的信號量叫做二元信號量(binary semaphore)。
|
||||
|
||||
<u><i>gopl.io/ch9/bank2</i></u>
|
||||
```go
|
||||
gopl.io/ch9/bank2
|
||||
var (
|
||||
sema = make(chan struct{}, 1) // a binary semaphore guarding balance
|
||||
balance int
|
||||
sema = make(chan struct{}, 1) // a binary semaphore guarding balance
|
||||
balance int
|
||||
)
|
||||
|
||||
func Deposit(amount int) {
|
||||
sema <- struct{}{} // acquire token
|
||||
balance = balance + amount
|
||||
<-sema // release token
|
||||
sema <- struct{}{} // acquire token
|
||||
balance = balance + amount
|
||||
<-sema // release token
|
||||
}
|
||||
|
||||
func Balance() int {
|
||||
sema <- struct{}{} // acquire token
|
||||
b := balance
|
||||
<-sema // release token
|
||||
return b
|
||||
sema <- struct{}{} // acquire token
|
||||
b := balance
|
||||
<-sema // release token
|
||||
return b
|
||||
}
|
||||
```
|
||||
|
||||
這種互斥很實用,而且被sync包里的Mutex類型直接支持。它的Lock方法能夠獲取到token(這里叫鎖),併且Unlock方法會釋放這個token:
|
||||
|
||||
<u><i>gopl.io/ch9/bank3</i></u>
|
||||
```go
|
||||
gopl.io/ch9/bank3
|
||||
import "sync"
|
||||
|
||||
var (
|
||||
mu sync.Mutex // guards balance
|
||||
balance int
|
||||
mu sync.Mutex // guards balance
|
||||
balance int
|
||||
)
|
||||
|
||||
func Deposit(amount int) {
|
||||
mu.Lock()
|
||||
balance = balance + amount
|
||||
mu.Unlock()
|
||||
mu.Lock()
|
||||
balance = balance + amount
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
func Balance() int {
|
||||
mu.Lock()
|
||||
b := balance
|
||||
mu.Unlock()
|
||||
return b
|
||||
mu.Lock()
|
||||
b := balance
|
||||
mu.Unlock()
|
||||
return b
|
||||
}
|
||||
```
|
||||
|
||||
@@ -58,9 +58,9 @@ func Balance() int {
|
||||
|
||||
```go
|
||||
func Balance() int {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
return balance
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
return balance
|
||||
}
|
||||
```
|
||||
|
||||
@@ -73,12 +73,12 @@ func Balance() int {
|
||||
```go
|
||||
// NOTE: not atomic!
|
||||
func Withdraw(amount int) bool {
|
||||
Deposit(-amount)
|
||||
if Balance() < 0 {
|
||||
Deposit(amount)
|
||||
return false // insufficient funds
|
||||
}
|
||||
return true
|
||||
Deposit(-amount)
|
||||
if Balance() < 0 {
|
||||
Deposit(amount)
|
||||
return false // insufficient funds
|
||||
}
|
||||
return true
|
||||
}
|
||||
```
|
||||
|
||||
@@ -89,14 +89,14 @@ func Withdraw(amount int) bool {
|
||||
```go
|
||||
// NOTE: incorrect!
|
||||
func Withdraw(amount int) bool {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
Deposit(-amount)
|
||||
if Balance() < 0 {
|
||||
Deposit(amount)
|
||||
return false // insufficient funds
|
||||
}
|
||||
return true
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
Deposit(-amount)
|
||||
if Balance() < 0 {
|
||||
Deposit(amount)
|
||||
return false // insufficient funds
|
||||
}
|
||||
return true
|
||||
}
|
||||
```
|
||||
|
||||
@@ -108,26 +108,26 @@ func Withdraw(amount int) bool {
|
||||
|
||||
```go
|
||||
func Withdraw(amount int) bool {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
deposit(-amount)
|
||||
if balance < 0 {
|
||||
deposit(amount)
|
||||
return false // insufficient funds
|
||||
}
|
||||
return true
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
deposit(-amount)
|
||||
if balance < 0 {
|
||||
deposit(amount)
|
||||
return false // insufficient funds
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func Deposit(amount int) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
deposit(amount)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
deposit(amount)
|
||||
}
|
||||
|
||||
func Balance() int {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
return balance
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
return balance
|
||||
}
|
||||
|
||||
// This function requires that the lock be held.
|
||||
|
||||
Reference in New Issue
Block a user