# 致命错误：所有 goroutine 都处于休眠状态 - 死锁！（RWMutex 写锁）

- **ID:** `go/rwmutex-write-lock-deadlock`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

持有读锁的 goroutine 尝试获取写锁，导致死锁，因为写锁需要等待所有读锁释放。

## 版本兼容性

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

## 解决方案

1. **Avoid holding read lock when acquiring write lock; release read lock first** (95% 成功率)
   ```
   rw.RUnlock()
rw.Lock()
// write
rw.Unlock()
rw.RLock() // if needed again
   ```
2. **Use a single mutex instead of RWMutex if write locks are frequent** (90% 成功率)
   ```
   var mu sync.Mutex
mu.Lock()
// write
mu.Unlock()
   ```

## 无效尝试

- **Using a separate mutex for writes** — Does not address the recursive locking issue; may still deadlock. (80% 失败率)
- **Increasing the number of goroutines** — More goroutines can exacerbate the deadlock. (90% 失败率)
