# 致命错误：对未对齐内存的并发原子操作

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

## 根因

对未正确对齐的变量使用原子操作，在某些架构（如 ARM）上导致恐慌。

## 版本兼容性

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

## 解决方案

1. **Ensure atomic variables are 64-bit aligned, e.g., by using a struct with proper padding.** (95% 成功率)
   ```
   type Aligned struct {
    _    [0]int64 // ensure alignment
    val  int64
}
var a Aligned
atomic.StoreInt64(&a.val, 42)
fmt.Println(atomic.LoadInt64(&a.val))
   ```
2. **Use sync.Mutex instead of atomic for non-critical paths.** (90% 成功率)
   ```
   var mu sync.Mutex
var val int64
mu.Lock()
val = 42
mu.Unlock()
mu.Lock()
fmt.Println(val)
mu.Unlock()
   ```

## 无效尝试

- **Assuming atomic operations work on any variable regardless of alignment.** — Some platforms require 64-bit alignment for 64-bit atomics; misalignment causes panic. (90% 失败率)
- **Using a struct with atomic fields without ensuring alignment.** — Fields may be packed in a way that causes misalignment. (80% 失败率)
