Files
XCSDD/docs/api/threading/mutex/mutex.md
ssdfasd 58a83f445a fix: improve doc link navigation and tree display
- Fix link resolution with proper relative/absolute path handling
- Improve link styling with underline decoration
- Hide leaf nodes from tree, only show directories
- Fix log file path for packaged app
2026-03-19 12:44:08 +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) - 模块总览