# 恐慌：同步：互斥锁已锁定（复制值）

- **ID:** `go/mutex-copy-value`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

sync.Mutex（或其他同步原语）在锁定后按值复制。互斥锁不应被复制；复制已锁定的互斥锁会导致未定义行为，并可能引起恐慌。

## 版本兼容性

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

## 解决方案

1. **Use a pointer to the struct that contains the mutex** (95% 成功率)
   ```
   type SafeStruct struct {
    mu sync.Mutex
    data int
}
func NewSafeStruct() *SafeStruct {
    return &SafeStruct{}
}
   ```
2. **Embed the mutex as a pointer field** (90% 成功率)
   ```
   type SafeStruct struct {
    mu   *sync.Mutex
    data int
}
func NewSafeStruct() *SafeStruct {
    return &SafeStruct{mu: &sync.Mutex{}}
}
   ```

## 无效尝试

- **Using a pointer to the mutex but still copying it** — If the struct containing the mutex is copied, the mutex is still copied even if it's a pointer field; the pointer itself is copied, but the underlying mutex is shared incorrectly. (85% 失败率)
- **Ignoring the panic and continuing execution** — The panic is fatal and will crash the program; ignoring it is not possible without recovery, and even then the mutex state is corrupted. (95% 失败率)
