Delayed Function Call

我的梦境 提交于 2019-12-04 22:13:00

You mean something like this?

#include <iostream>
#include <chrono>
#include <thread>
#include <future>

int main()
{
    // Use async to launch a function (lambda) in parallel
    std::async(std::launch::async, [] () {
        // Use sleep_for to wait specified time (or sleep_until).
        std::this_thread::sleep_for( std::chrono::seconds{1});
        // Do whatever you want.
        std::cout << "Lights out!" << std::endl;
    } );
    std::this_thread::sleep_for( std::chrono::seconds{2});
    std::cout << "Finished" << std::endl;
}

Just make sure that you don't capture a variable by reference in the lambda.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!