# 警告：数据竞争：协程X写入，协程Y读取（原子变量）

- **ID:** `go/goroutine-atomic-load-store-mismatch`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

对同时被非原子访问的变量使用atomic.Load/Store，导致竞争。

## 版本兼容性

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

## 解决方案

1. **Use atomic operations consistently for all accesses** (95% 成功率)
   ```
   var val atomic.Value
val.Store(42)
v := val.Load().(int)
   ```
2. **Use sync.Mutex to protect all accesses** (90% 成功率)
   ```
   var mu sync.Mutex
var x int
mu.Lock()
x = 42
mu.Unlock()
mu.Lock()
v := x
mu.Unlock()
   ```

## 无效尝试

- **Using volatile keyword (not available in Go)** — Go doesn't have volatile; atomic operations are required. (100% 失败率)
- **Adding memory barrier manually** — Go's memory model is complex; manual barriers are error-prone. (70% 失败率)
