Files
XCSDD/docs/api/threading/task-system/runonmainthread.md
ssdfasd 58a83f445a fix: improve doc link navigation and tree display
- Fix link resolution with proper relative/absolute path handling
- Improve link styling with underline decoration
- Hide leaf nodes from tree, only show directories
- Fix log file path for packaged app
2026-03-19 12:44:08 +08:00

879 B

TaskSystem::RunOnMainThread

void RunOnMainThread(std::function<void()>&& func)

将任务提交到主线程队列。在主线程中调用 Update 时执行。

参数:

  • func - 要在主线程执行的函数

返回:

复杂度: O(1)

使用场景:

  • 从工作线程回调需要更新 UI 或访问主线程资源。
  • 避免跨线程数据竞争。

示例:

// 在工作线程中
void WorkerThreadCode() {
    int result = HeavyCompute();
    
    // 将结果发送到主线程更新 UI
    TaskSystem::Get().RunOnMainThread([result]() {
        UI.UpdateResult(result);
    });
}

// 在主线程中
while (running) {
    TaskSystem::Get().Update(); // 处理主线程任务
    RenderFrame();
}

相关文档