Files
XCEngine/docs/api/threading/mutex/mutex.md
ssdfasd 8df04c120f docs: 更新 API 文档 - 多模块修复和完善
- audio: 更新 audio-system 方法文档
- components: 新增 audio-listener/audio-source 组件方法文档,新增 remove-component 方法
- core: 更新 filewriter, types 文档
- math: 更新 box 方法文档
- memory: 更新 proxy-allocator 文档
- resources: 更新 loader 和 texture 文档
- rhi: 更新 opengl 设备、shader、swap-chain 文档
- threading: 更新 mutex 和 task-system 文档
2026-03-26 01:58:45 +08:00

64 lines
1.6 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/unlock/try_lock`STL 兼容接口const 成员函数)
## 公共方法
| 方法 | 描述 |
|------|------|
| [`Lock`](lock.md) | 获取互斥锁(非 const |
| [`Unlock`](unlock.md) | 释放互斥锁(非 const |
| [`TryLock`](trylock.md) | 尝试获取互斥锁(非 const |
| [`lock`](lock-const.md) | 获取互斥锁constSTL 兼容) |
| [`unlock`](unlock-const.md) | 释放互斥锁constSTL 兼容) |
| [`try_lock`](try-lock-const.md) | 尝试获取互斥锁constSTL 兼容) |
## 使用示例
```cpp
#include "XCEngine/Threading/Mutex.h"
#include <thread>
#include <iostream>
XCEngine::Threading::Mutex mutex;
int sharedData = 0;
void increment() {
mutex.lock();
++sharedData;
mutex.unlock();
}
int main() {
std::thread t1(increment);
std::thread t2(increment);
t1.join();
t2.join();
std::cout << "sharedData = " << sharedData << std::endl;
return 0;
}
```
## 相关文档
- [SpinLock](../spinlock/spinlock.md) - 自旋锁
- [ReadWriteLock](../read-write-lock/read-write-lock.md) - 读写锁
- [TaskSystem](../task-system/task-system.md) - 任务系统
- [../threading.md](../threading.md) - 模块总览