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

1.6 KiB
Raw Blame History

Mutex

命名空间: XCEngine::Threading

类型: class

头文件: XCEngine/Threading/Mutex.h

描述: 互斥锁,封装 std::mutex 提供线程同步

概述

Mutex 类是对 std::mutex 的封装,提供线程安全的互斥访问机制。该类确保同一时刻只有一个线程可以访问受保护的资源,防止数据竞争和竞态条件。

该类提供两套接口:

  • Lock/Unlock/TryLockXCEngine 风格接口
  • lock/unlock/try_lockSTL 兼容接口const 成员函数)

公共方法

方法 描述
Lock 获取互斥锁(非 const
Unlock 释放互斥锁(非 const
TryLock 尝试获取互斥锁(非 const
lock 获取互斥锁constSTL 兼容)
unlock 释放互斥锁constSTL 兼容)
try_lock 尝试获取互斥锁constSTL 兼容)

使用示例

#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;
}

相关文档