# 警告：数据竞争：协程X写入，协程Y读取（sync.Pool）

- **ID:** `go/goroutine-sync-pool-race`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

未正确理解sync.Pool的使用；池中的对象可能在没有同步的情况下被并发访问。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.3 | active | — | — |
| 1.20 | active | — | — |

## 解决方案

1. **Ensure pooled objects are not shared between goroutines** (85% 成功率)
   ```
   pool := &sync.Pool{New: func() interface{} { return &MyStruct{} }}
go func() {
    obj := pool.Get().(*MyStruct)
    // use obj exclusively
    pool.Put(obj)
}()
   ```
2. **Use mutex inside pooled object if sharing is needed** (80% 成功率)
   ```
   type MyStruct struct {
    mu sync.Mutex
    data int
}
obj := pool.Get().(*MyStruct)
obj.mu.Lock()
obj.data = 42
obj.mu.Unlock()
pool.Put(obj)
   ```

## 无效尝试

- **Assuming sync.Pool provides synchronization** — sync.Pool does not synchronize access to pooled objects; only the pool itself is safe. (90% 失败率)
- **Using global variables instead of pool** — Global variables are shared; race condition persists. (80% 失败率)
