- 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
1.6 KiB
1.6 KiB
Mutex
命名空间: XCEngine::Threading
类型: class
头文件: XCEngine/Threading/Mutex.h
描述: 互斥锁,封装 std::mutex 提供线程同步
概述
Mutex 类是对 std::mutex 的封装,提供线程安全的互斥访问机制。该类确保同一时刻只有一个线程可以访问受保护的资源,防止数据竞争和竞态条件。
该类提供两套接口:
Lock/Unlock/TryLock:XCEngine 风格接口lock/unlock/try_lock:STL 兼容接口(const 成员函数)
公共方法
| 方法 | 描述 |
|---|---|
Lock |
获取互斥锁(非 const) |
Unlock |
释放互斥锁(非 const) |
TryLock |
尝试获取互斥锁(非 const) |
lock |
获取互斥锁(const,STL 兼容) |
unlock |
释放互斥锁(const,STL 兼容) |
try_lock |
尝试获取互斥锁(const,STL 兼容) |
使用示例
#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 - 自旋锁
- ReadWriteLock - 读写锁
- TaskSystem - 任务系统
- ../threading/threading.md - 模块总览