Files
XCEngine/docs/api/threading/mutex/mutex.md
ssdfasd 870cb3116e docs: 修正 API 文档准确性 (第三轮检查)
修复问题:
- containers: HashMap include 路径修复
- core: RefCounted 析构函数访问级别, Event::begin/end 返回值说明
- memory: LinearAllocator::GetMarker 返回偏移量非指针
- resources: LoadAsync 示例使用不存在的模板方法
- rhi: OpenGL 链接错误, ShaderType 枚举缺失8个类型, 链接修复
- threading: SpinLock STL 兼容方法说明, Mutex const 方法说明
2026-03-19 00:35:26 +08:00

55 lines
1.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Mutex
**命名空间**: `XCEngine::Threading`
**类型**: `class`
**头文件**: `XCEngine/Threading/Mutex.h`
**描述**: 互斥锁封装类,基于 `std::mutex` 实现,提供线程安全的互斥访问。
## 概述
`Mutex` 是对 `std::mutex` 的简单封装,提供了标准的 Lock/Unlock/TryLock 接口。它是 XCEngine 中最基础的同步原语,适用于保护共享数据的访问。
## 公共方法
| 方法 | 描述 |
|------|------|
| [`Lock`](lock.md) | 获取锁(阻塞) |
| [`Unlock`](unlock.md) | 释放锁 |
| [`TryLock`](trylock.md) | 尝试获取锁(非阻塞,成功返回 true |
## STL 兼容方法
支持 `lock()`, `unlock()`, `try_lock()` 以兼容 STL 的 lockable 概念(**注意**:这些方法为 const 成员函数)。
## 使用示例
```cpp
Threading::Mutex mtx;
int sharedData = 0;
// 线程安全地修改共享数据
void SafeIncrement() {
mtx.Lock();
++sharedData;
mtx.Unlock();
}
// 或使用 TryLock
void SafeTryIncrement() {
if (mtx.TryLock()) {
++sharedData;
mtx.Unlock();
}
}
```
## 相关文档
- [SpinLock](../spinlock/spinlock.md) - 自旋锁
- [ReadWriteLock](../readwritelock/readwritelock.md) - 读写锁
- [TaskSystem](../task-system/task-system.md) - 任务系统
- [../threading/threading.md](../threading.md) - 模块总览