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

1.2 KiB
Raw Blame History

Thread::Start

template<typename Func>
void Start(Func&& func, const Containers::String& name = "Thread");

启动一个新线程并执行传入的可调用对象。该方法是模板方法可以接受任何可调用对象包括普通函数、Lambda 表达式、仿函数等。

调用 Start() 后,线程立即开始执行。线程 ID 通过 native_handle() 获取并转换为 uint64_t 类型。

注意: Start() 不是线程安全的,不应在同一 Thread 对象上并发调用。

参数:

  • func - 要执行的线程函数,可以是任意可调用对象
  • name - 线程名称,默认为 "Thread",用于调试和日志记录

返回:

线程安全:

示例:

#include "XCEngine/Threading/Thread.h"
#include <iostream>

using namespace XCEngine::Threading;

void FreeFunction(int value) {
    std::cout << "Free function called with: " << value << std::endl;
}

int main() {
    Thread t;
    t.Start(FreeFunction, "FreeFuncThread");
    t.Start([]() {
        std::cout << "Lambda thread executing" << std::endl;
    }, "LambdaThread");
    t.Join();
    return 0;
}

相关文档