docs: update memory and threading API docs

This commit is contained in:
2026-03-20 02:35:24 +08:00
parent c5b17239ca
commit fd792b7df1
103 changed files with 2485 additions and 673 deletions

View File

@@ -19,7 +19,7 @@ void Lock()
**示例:**
```cpp
Threading::Mutex mtx;
XCEngine::Threading::Mutex mtx;
int counter = 0;
void Increment() {

View File

@@ -0,0 +1,43 @@
# Mutex::lock
```cpp
void lock() const;
```
获取互斥锁const 版本)。如果锁已被其他线程持有,则阻塞当前线程直到锁可用。
该方法是 `std::lockable` 接口的实现,可与 `std::lock_guard``std::unique_lock` 等 RAII 工具配合使用。
**参数:**
**返回:**
**线程安全:**
**复杂度:** 平均 O(1),最坏情况 O(n)n 为竞争线程数
**示例:**
```cpp
#include "XCEngine/Threading/Mutex.h"
#include <mutex>
XCEngine::XCEngine::Threading::Mutex mtx;
int counter = 0;
void Increment() {
mtx.lock();
++counter;
mtx.unlock();
}
// 使用 RAII 自动管理锁
void SafeIncrement() {
std::lock_guard<XCEngine::Threading::Mutex> guard(mtx);
++counter;
}
```
## 相关文档
- [Mutex 总览](mutex.md) - 返回类总览

View File

@@ -6,43 +6,52 @@
**头文件**: `XCEngine/Threading/Mutex.h`
**描述**: 互斥锁封装类,基于 `std::mutex` 实现,提供线程安全的互斥访问。
**描述**: 互斥锁封装 std::mutex 提供线程同步
## 概述
`Mutex` 是对 `std::mutex` 的简单封装,提供了标准的 Lock/Unlock/TryLock 接口。它是 XCEngine 中最基础的同步原语,适用于保护共享数据的访问
Mutex 是对 std::mutex 的封装,提供线程安全的互斥访问机制。该类确保同一时刻只有一个线程可以访问受保护的资源,防止数据竞争和竞态条件
该类提供两套接口:
- `Lock/Unlock/TryLock`XCEngine 风格接口
- `lock/unlock/try_lock`STL 兼容接口const 成员函数)
## 公共方法
| 方法 | 描述 |
|------|------|
| [`Lock`](lock.md) | 获取锁(阻塞 |
| [`Unlock`](unlock.md) | 释放 |
| [`TryLock`](trylock.md) | 尝试获取锁(非阻塞,成功返回 true |
## STL 兼容方法
支持 `lock()`, `unlock()`, `try_lock()` 以兼容 STL 的 lockable 概念。**注意**:这些方法为 `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
Threading::Mutex mtx;
#include "XCEngine/Threading/Mutex.h"
#include <thread>
#include <iostream>
XCEngine::Threading::Mutex mutex;
int sharedData = 0;
// 线程安全地修改共享数据
void SafeIncrement() {
mtx.Lock();
void increment() {
mutex.lock();
++sharedData;
mtx.Unlock();
mutex.unlock();
}
// 或使用 TryLock
void SafeTryIncrement() {
if (mtx.TryLock()) {
++sharedData;
mtx.Unlock();
}
int main() {
std::thread t1(increment);
std::thread t2(increment);
t1.join();
t2.join();
std::cout << "sharedData = " << sharedData << std::endl;
return 0;
}
```

View File

@@ -0,0 +1,36 @@
# Mutex::try_lock
```cpp
bool try_lock() const;
```
尝试获取互斥锁,非阻塞。如果锁不可用,调用线程不会阻塞,而是立即返回。
**参数:**
**返回:** `bool` - 如果成功获取锁返回 `true`,否则返回 `false`
**线程安全:**
**示例:**
```cpp
#include "XCEngine/Threading/Mutex.h"
#include <iostream>
XCEngine::Threading::Mutex mutex;
void tryOperation() {
if (mutex.try_lock()) {
// 获取锁成功
mutex.unlock();
} else {
// 锁已被其他线程持有
std::cout << "Lock not available" << std::endl;
}
}
```
## 相关文档
- [Mutex 类总览](mutex.md) - 返回类总览

View File

@@ -0,0 +1,41 @@
# Mutex::try_lock
```cpp
bool try_lock() const;
```
尝试获取互斥锁非阻塞const 版本)。如果锁可用则立即获取并返回 `true`,否则立即返回 `false` 而不阻塞。
该方法是 `std::lockable` 接口的实现。
**参数:**
**返回:** `bool` - 获取成功返回 `true`,锁不可用返回 `false`
**线程安全:**
**复杂度:** O(1)
**示例:**
```cpp
#include "XCEngine/Threading/Mutex.h"
#include <iostream>
XCEngine::XCEngine::Threading::Mutex mtx;
volatile bool updated = false;
void TryUpdate() {
if (mtx.try_lock()) {
updated = true;
mtx.unlock();
std::cout << "Update succeeded" << std::endl;
} else {
std::cout << "Update skipped (lock held)" << std::endl;
}
}
```
## 相关文档
- [Mutex 总览](mutex.md) - 返回类总览

View File

@@ -17,7 +17,7 @@ bool TryLock()
**示例:**
```cpp
Threading::Mutex mtx;
XCEngine::Threading::Mutex mtx;
volatile bool updated = false;
void TryUpdate() {

View File

@@ -17,7 +17,7 @@ void Unlock()
**示例:**
```cpp
Threading::Mutex mtx;
XCEngine::Threading::Mutex mtx;
std::vector<int> data;
void SafePush(int value) {

View File

@@ -0,0 +1,34 @@
# Mutex::unlock
```cpp
void unlock() const;
```
释放互斥锁const 版本),允许其他等待中的线程获取该锁。
**参数:**
**返回:**
**线程安全:**
**复杂度:** O(1)
**示例:**
```cpp
#include "XCEngine/Threading/Mutex.h"
XCEngine::XCEngine::Threading::Mutex mtx;
std::vector<int> data;
void SafePush(int value) {
mtx.lock();
data.push_back(value);
mtx.unlock();
}
```
## 相关文档
- [Mutex 总览](mutex.md) - 返回类总览