947 B
947 B
TaskSystem::RunOnMainThread
void RunOnMainThread(std::function<void()>&& func)
将任务提交到主线程队列。在主线程中调用 Update 时执行。
参数:
func- 要在主线程执行的函数
返回: 无
线程安全: ✅ (内部使用 mutex 保护主线程队列)
复杂度: O(1)
使用场景:
- 从工作线程回调需要更新 UI 或访问主线程资源。
- 避免跨线程数据竞争。
示例:
// 在工作线程中
void WorkerThreadCode() {
int result = HeavyCompute();
// 将结果发送到主线程更新 UI
TaskSystem::Get().RunOnMainThread([result]() {
UI.UpdateResult(result);
});
}
// 在主线程中
while (running) {
TaskSystem::Get().Update(); // 处理主线程任务
RenderFrame();
}
相关文档
- TaskSystem 总览 - 返回类总览
- Update - 处理主线程队列