Files
XCEngine/docs/api/threading/mutex/mutex.md
ssdfasd f427699ac6 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

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/threading.md](../threading.md) - 模块总览