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

48 lines
1.2 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::Join
```cpp
void Join();
```
等待线程执行完成并回收其资源。调用 `Join()` 会阻塞当前线程,直到目标线程结束运行。如果目标线程已经结束或从未启动,则此调用立即返回。
`Join()` 只能调用一次。在调用 `Join()` 后,`std::thread` 对象不再代表任何线程,不能再次调用 `Join()``Detach()`
析构函数会自动调用 `Join()`,确保线程对象销毁前等待线程结束。
**注意:** 如果线程已经被分离Detach或已经 Join 过,调用此方法将导致未定义行为。
**参数:**
**返回:**
**线程安全:**
**示例:**
```cpp
#include "XCEngine/Threading/Thread.h"
#include <iostream>
using namespace XCEngine::Threading;
int main() {
Thread t;
t.Start([]() {
for (int i = 0; i < 3; ++i) {
std::cout << "Working... " << i << std::endl;
Thread::Sleep(100);
}
}, "WorkerThread");
std::cout << "Waiting for thread to complete..." << std::endl;
t.Join();
std::cout << "Thread completed!" << std::endl;
return 0;
}
```
## 相关文档
- [Thread 总览](thread.md) - 返回类总览