std::thread用法简记

//=============================================================================================================================
// thread对象一定要join,否则Debug会报错:"abort() has been called",Release不报错
//=============================================================================================================================
void Test1()
{
    std::cout << "\n=============================== Test1 ===============================\n";
    int res;
    thread t([&res] (int x)
    {
        std::cout << "进入线程\n";
        this_thread::sleep_for(chrono::seconds(2));
        res = 3 + 4 + x;
    }, 10);
    std::cout << "主线程挂起\n";

    // 如果没有这一行,Debug会报错:"abort() has been called",Release不报错
    t.join();

    std::cout << "任务完成,结果:" << res << endl;
}

 

上一篇:std::async用法简记


下一篇:std::counting_semaphore 练习