Files
the-way-to-go_ZH_CN/eBook/14.17.md
2022-05-17 16:26:06 +08:00

76 lines
2.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 14.17 使用通道并发访问对象
为了保护对象被并发访问修改,我们可以使用协程在后台顺序执行匿名函数来替代使用同步互斥锁。在下面的程序中我们有一个类型 `Person` 中包含一个字段 `chF` ,这是一个用于存放匿名函数的通道。
这个结构在构造函数 `NewPerson()` 中初始化的同时会启动一个后台协程 `backend()``backend()` 方法会在一个无限循环中执行 `chF` 中放置的所有函数,有效地将它们序列化从而提供了安全的并发访问。更改和读取 `salary` 的方法会通过将一个匿名函数写入 `chF` 通道中,然后让 `backend()` 按顺序执行以达到其目的。需注意的是 `Salary()` 方法创建的闭包函数是如何将 `fChan` 通道包含在其中的。
当然,这是一个简化的例子,它不应该被用在这种案例下。但是它却向我们展示了在更复杂的场景中该如何解决这种问题。
示例14.19-[conc_access.go](examples/chapter_14/conc_access.go)
```go
package main
import (
"fmt"
"strconv"
)
type Person struct {
Name string
salary float64
chF chan func()
}
func NewPerson(name string, salary float64) *Person {
p := &Person{name, salary, make(chan func())}
go p.backend()
return p
}
func (p *Person) backend() {
for f := range p.chF {
f()
}
}
// Set salary.
func (p *Person) SetSalary(sal float64) {
p.chF <- func() { p.salary = sal }
}
// Retrieve salary.
func (p *Person) Salary() float64 {
fChan := make(chan float64)
p.chF <- func() { fChan <- p.salary }
return <-fChan
}
func (p *Person) String() string {
return "Person - name is: " + p.Name + " - salary is: " + strconv.FormatFloat(p.Salary(), 'f', 2, 64)
}
func main() {
bs := NewPerson("Smith Bill", 2500.5)
fmt.Println(bs)
bs.SetSalary(4000.25)
fmt.Println("Salary changed:")
fmt.Println(bs)
}
```
输出:
```
Person - name is: Smith Bill - salary is: 2500.50
Salary changed:
Person - name is: Smith Bill - salary is: 4000.25
```
## 链接
- [目录](directory.md)
- 上一节:[对Go协程进行基准测试](14.16.md)
- 下一章:[网络,模板和网页应用](15.0.md)