Files
XCEngine/docs/api/XCEngine/Threading/Thread/Thread.md

47 lines
1.9 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.
# Thread
**命名空间**: `XCEngine::Threading`
**类型**: `class`
**头文件**: `XCEngine/Threading/Thread.h`
**描述**: 对 `std::thread` 的轻量包装,附带名称字符串和一个缓存的线程标识值。
## 概述
`Thread` 不是线程系统,而是一个非常薄的 `std::thread` 外壳。它主要额外做了两件事:
- 保存一个 `Containers::String m_name`
-`Start()` 时缓存一个 `m_id`
这类包装在引擎里很常见,因为很多系统希望在不直接暴露标准库类型的前提下,统一线程 API、线程名和工具层调试信息。
## 当前实现边界
- [Destructor](Destructor.md) 会在对象析构时自动 `Join()`,因此析构可能阻塞。
- [Start](Start.md) 只保存名称字符串,不会设置 OS 层面的线程名。
- [GetId](GetId.md) 的来源是 `native_handle()` 转成整数,而 [GetCurrentId](GetCurrentId.md) 的来源是 `std::thread::id` 的 hash两者不是同一口径。
- [Start](Start.md) 当前没有防御“对一个仍然 joinable 的线程再次 Start”的情况这会落到 `std::thread` 的终止语义。
- 当前没有对应单测覆盖。
## 公开方法
| 方法 | 说明 |
|------|------|
| [Constructor](Constructor.md) | 构造空线程对象。 |
| [Destructor](Destructor.md) | 析构时自动 join。 |
| [Start](Start.md) | 启动线程。 |
| [Join](Join.md) | 等待线程结束。 |
| [Detach](Detach.md) | 分离线程。 |
| [GetId](GetId.md) | 获取缓存的线程标识。 |
| [GetName](GetName.md) | 获取保存的线程名称。 |
| [GetCurrentId](GetCurrentId.md) | 获取当前线程标识。 |
| [Sleep](Sleep.md) | 让当前线程休眠。 |
| [Yield](Yield.md) | 让出当前线程时间片。 |
## 相关文档
- [当前模块](../Threading.md)
- [Synchronization And Task System Limits](../../../_guides/Threading/Synchronization-And-TaskSystem-Limits.md)