2026-03-18 17:49:22 +08:00
|
|
|
|
# Mutex
|
|
|
|
|
|
|
|
|
|
|
|
**命名空间**: `XCEngine::Threading`
|
|
|
|
|
|
|
|
|
|
|
|
**类型**: `class`
|
|
|
|
|
|
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
|
**头文件**: `XCEngine/Threading/Mutex.h`
|
|
|
|
|
|
|
2026-03-20 02:35:24 +08:00
|
|
|
|
**描述**: 互斥锁,封装 std::mutex 提供线程同步
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
|
|
## 概述
|
|
|
|
|
|
|
2026-03-20 02:35:24 +08:00
|
|
|
|
Mutex 类是对 std::mutex 的封装,提供线程安全的互斥访问机制。该类确保同一时刻只有一个线程可以访问受保护的资源,防止数据竞争和竞态条件。
|
|
|
|
|
|
|
|
|
|
|
|
该类提供两套接口:
|
|
|
|
|
|
- `Lock/Unlock/TryLock`:XCEngine 风格接口
|
|
|
|
|
|
- `lock/unlock/try_lock`:STL 兼容接口(const 成员函数)
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
|
|
## 公共方法
|
|
|
|
|
|
|
|
|
|
|
|
| 方法 | 描述 |
|
|
|
|
|
|
|------|------|
|
2026-03-20 02:35:24 +08:00
|
|
|
|
| [`Lock`](lock.md) | 获取互斥锁(非 const) |
|
|
|
|
|
|
| [`Unlock`](unlock.md) | 释放互斥锁(非 const) |
|
|
|
|
|
|
| [`TryLock`](trylock.md) | 尝试获取互斥锁(非 const) |
|
refactor: improve test infrastructure and fix OpenGL GLAD initialization
- Rename D3D12Enum.h to D3D12Enums.h for naming consistency
- Fix OpenGL unit test GLAD initialization by using gladLoadGL()
instead of gladLoadGLLoader(wglGetProcAddress) for fallback support
- Migrate remaining tests to use gtest_discover_tests for granular
test discovery (math, core, containers, memory, threading, debug,
components, scene, resources, input, opengl)
- Remove obsolete TEST_RESOURCES_DIR and copy_directory commands
from OpenGL unit test CMakeLists (minimal/Res doesn't exist)
- Update TEST_SPEC.md with performance metrics and per-module
build/test commands for faster development workflow
- Update CMake path references to use lowercase paths
2026-03-23 00:43:02 +08:00
|
|
|
|
| [`lock`](lock-const.md) | 获取互斥锁(const,STL 兼容) |
|
|
|
|
|
|
| [`unlock`](unlock-const.md) | 释放互斥锁(const,STL 兼容) |
|
|
|
|
|
|
| [`try_lock`](try-lock-const.md) | 尝试获取互斥锁(const,STL 兼容) |
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
|
|
## 使用示例
|
|
|
|
|
|
|
|
|
|
|
|
```cpp
|
2026-03-20 02:35:24 +08:00
|
|
|
|
#include "XCEngine/Threading/Mutex.h"
|
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
|
|
XCEngine::Threading::Mutex mutex;
|
2026-03-18 17:49:22 +08:00
|
|
|
|
int sharedData = 0;
|
|
|
|
|
|
|
2026-03-20 02:35:24 +08:00
|
|
|
|
void increment() {
|
|
|
|
|
|
mutex.lock();
|
2026-03-18 17:49:22 +08:00
|
|
|
|
++sharedData;
|
2026-03-20 02:35:24 +08:00
|
|
|
|
mutex.unlock();
|
2026-03-18 17:49:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-20 02:35:24 +08:00
|
|
|
|
int main() {
|
|
|
|
|
|
std::thread t1(increment);
|
|
|
|
|
|
std::thread t2(increment);
|
|
|
|
|
|
|
|
|
|
|
|
t1.join();
|
|
|
|
|
|
t2.join();
|
|
|
|
|
|
|
|
|
|
|
|
std::cout << "sharedData = " << sharedData << std::endl;
|
|
|
|
|
|
return 0;
|
2026-03-18 17:49:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 相关文档
|
|
|
|
|
|
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
|
- [SpinLock](../spinlock/spinlock.md) - 自旋锁
|
refactor: improve test infrastructure and fix OpenGL GLAD initialization
- Rename D3D12Enum.h to D3D12Enums.h for naming consistency
- Fix OpenGL unit test GLAD initialization by using gladLoadGL()
instead of gladLoadGLLoader(wglGetProcAddress) for fallback support
- Migrate remaining tests to use gtest_discover_tests for granular
test discovery (math, core, containers, memory, threading, debug,
components, scene, resources, input, opengl)
- Remove obsolete TEST_RESOURCES_DIR and copy_directory commands
from OpenGL unit test CMakeLists (minimal/Res doesn't exist)
- Update TEST_SPEC.md with performance metrics and per-module
build/test commands for faster development workflow
- Update CMake path references to use lowercase paths
2026-03-23 00:43:02 +08:00
|
|
|
|
- [ReadWriteLock](../read-write-lock/read-write-lock.md) - 读写锁
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
|
- [TaskSystem](../task-system/task-system.md) - 任务系统
|
|
|
|
|
|
- [../threading/threading.md](../threading.md) - 模块总览
|