# FreeRTOS：任务'SensorTask'中检测到互斥锁死锁，等待信号量0x2000ABCD，由'ControlTask'持有（已阻塞5000毫秒）

- **ID:** `embedded/freertos-mutex-deadlock`
- **领域:** embedded
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 88%

## 根因

两个或多个任务无限期等待对方持有的互斥锁，通常由于锁定顺序不正确或在优先级反转场景中缺少超时引起。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| FreeRTOS Kernel V10.5.1 | active | — | — |
| FreeRTOS Kernel V11.0.0 | active | — | — |
| STM32Cube_FW_F4_V1.27.1 | active | — | — |

## 解决方案

1. ```
   Implement a consistent lock ordering: always acquire mutexes in the same order (e.g., SensorMutex before ControlMutex) across all tasks.
   ```
2. ```
   Add a timeout to semaphore takes (e.g., 100 ms) and release all held mutexes on timeout, then retry, to break deadlocks.
   ```
3. ```
   Use a recursive mutex if a single task may re-acquire the same mutex (e.g., via nested function calls).
   ```

## 无效尝试

- **** — Deadlock is a synchronization issue, not a memory issue; larger stacks don't resolve lock ordering conflicts. (95% 失败率)
- **** — This hides the symptom but the system remains hung; tasks never progress. (100% 失败率)
- **** — Binary semaphores don't prevent priority inversion or deadlock; the same circular dependency persists. (85% 失败率)
