首页 > 编程语言 >c++ std::package_task,task.get_future()

c++ std::package_task,task.get_future()

时间:2023-04-03 22:55:24浏览次数:39  
标签:std task get int future include

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

int countdown(int from,int to)
{
    for(int i=from;i!=to;--i)
    {
        std::cout<<i<<std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
    std::cout<<"Lift off!"<<std::endl;
    return from-to;
}

void package_task_get_future()
{
    std::packaged_task<int(int,int)> tsk(countdown); 
    std::future<int> fut=tsk.get_future();
    std::thread t1(std::move(tsk),10,0);
    int value=fut.get();
    std::cout<<"The countdown lasted for "<<value<<" seconds"<<std::endl;
    t1.join();
}

int main(int args, char **argv)
{
    package_task_get_future();
}

 

标签:std,task,get,int,future,include
From: https://www.cnblogs.com/Fred1987/p/17284798.html

相关文章