Files
XCEngine/docs/api/threading/thread/getcurrentid.md

44 lines
1.1 KiB
Markdown

# Thread::GetCurrentId
```cpp
static Id GetCurrentId();
```
静态方法,返回当前执行线程的唯一标识符。此方法可在任意线程上下文中调用,包括主线程和工作线程。
线程 ID 通过 `std::this_thread::get_id()` 获取,并使用 `std::hash<std::thread::id>` 转换为 `uint64_t` 值。该值与 `Thread::GetId()` 返回的值格式一致,可以直接比较。
**参数:**
**返回:** 调用线程的唯一标识符
**线程安全:**
**示例:**
```cpp
#include "XCEngine/Threading/Thread.h"
#include <iostream>
using namespace XCEngine::Threading;
int main() {
std::cout << "Main thread ID: " << Thread::GetCurrentId() << std::endl;
Thread t;
t.Start([]() {
std::cout << "Worker thread ID: " << Thread::GetCurrentId() << std::endl;
std::cout << "Worker ID matches Thread.GetId(): "
<< (Thread::GetCurrentId() == t.GetId() ? "Yes" : "No") << std::endl;
}, "CheckIdThread");
t.Join();
return 0;
}
```
## 相关文档
- [Thread 总览](thread.md) - 返回类总览
- [GetId](getid.md) - 获取 Thread 对象的 ID