# 致命错误：并发读 map 和写 map

- **ID:** `go/concurrent-map-read-and-write`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

一个 goroutine 读 map 的同时另一个 goroutine 写同一个 map。运行时检测到这种不一致访问会终止程序。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.18 | active | — | — |
| 1.21 | active | — | — |

## 解决方案

1. **** (93% 成功率)
   ```
   var mu sync.RWMutex
// writer
mu.Lock(); m[k]=v; mu.Unlock()
// reader
mu.RLock(); v := m[k]; mu.RUnlock()
   ```
2. **** (85% 成功率)
   ```
   type req struct{ key string; resp chan int }
// single owner goroutine processes reqs serially
   ```

## 无效尝试

- **** — Copying a map being concurrently written also races and may panic or produce corrupted data. (85% 失败率)
- **** — Timing-based synchronization is unreliable and breaks under load or different GOMAXPROCS. (87% 失败率)
