C++ async异步 *** 作

C++ async异步 *** 作,第1张

C++ async异步 *** 作

flyfish

文章目录
  • C++ async异步 *** 作
    • 一个简单的例子
    • lambda方式
    • 调用类的成员函数的方式
    • 延迟运行
    • 查看状态
    • 需要注意的问题

一个简单的例子
#include 
#include 
#include 

int for_a_long_time(int n)//s
{
    std::cout<<"for_a_long_time:begin"< f = std::async(for_a_long_time, 5);//s
    std::cout << "please wait"<

输出

please wait
for_a_long_time:begin
I am waiting.
I am waiting.
I am waiting.
I am waiting.
for_a_long_time:end
result: 0
lambda方式
#include 
#include 
#include 

int main()
{

    std::future f = std::async(std::launch::async, [](int n)
    {
        std::cout<<"for_a_long_time:begin"<
调用类的成员函数的方式
#include 
#include 
#include 

class my_class
{
public:
    int for_a_long_time(int n)//s
    {
        std::cout<<"for_a_long_time:begin"<<std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(n));
        std::cout<<"for_a_long_time:end"<<std::endl;
        return 0;
    }
};


int main()
{
    my_class my_object;
    std::future<int> f = std::async(std::launch::async,&my_class::for_a_long_time, my_object,5);//s
    std::cout << "please wait"<<std::endl;
    std::chrono::seconds span(1);//s
    while (f.wait_for(span) != std::future_status::ready)
        std::cout << "I am waiting."<<std::endl;
    int ret = f.get();
    std::cout << "result: " << ret<< std::endl;
    return 0;
}

输出

please wait
for_a_long_time:begin
I am waiting.
I am waiting.
I am waiting.
I am waiting.
for_a_long_time:end
result: 0
延迟运行

等3秒后for_a_long_time开始运行

#include 
#include 
#include 

class my_class
{
public:
    int for_a_long_time(int n)//s
    {
        std::cout<<"for_a_long_time:begin"<<std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(n));
        std::cout<<"for_a_long_time:end"<<std::endl;
        return 0;
    }
};


int main()
{
    my_class my_object;
    std::future<int> f = std::async(std::launch::deferred,&my_class::for_a_long_time, my_object,5);//s
    std::cout << "please wait"<<std::endl;

    std::this_thread::sleep_for(std::chrono::seconds(3));
    int ret = f.get();//
    std::cout << "result: " << ret<< std::endl;
    return 0;
}

输出

please wait
for_a_long_time:begin
for_a_long_time:end
result: 0
查看状态
#include 
#include 
#include 

class my_class
{
public:
    int for_a_long_time(int n)//s
    {
        std::cout<<"for_a_long_time:begin"<<std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(n));
        std::cout<<"for_a_long_time:end"<<std::endl;
        return 0;
    }
};


int main()
{
    my_class my_object;
    std::future<int> f = std::async(std::launch::async,&my_class::for_a_long_time, my_object,5);//s
    std::cout << "please wait"<<std::endl;



    std::future_status status;
    do
    {
        status = f.wait_for(std::chrono::seconds(1));

        if (status == std::future_status::ready)
        {
            std::cout << "std::future_status::ready: " << f.get() << std::endl;
        }
        else if (status == std::future_status::timeout)
        {
            std::cout << "std::future_status::timeout..." << std::endl;
        }
    } while (status != std::future_status::ready);

    std::cout << "over"<< std::endl;
    return 0;
}

输出

please wait
for_a_long_time:begin
std::future_status::timeout...
std::future_status::timeout...
std::future_status::timeout...
std::future_status::timeout...
for_a_long_time:end
std::future_status::ready: 0
over
需要注意的问题

std::future f1延长了std::async返回值的生命周期,所以是异步

#include 
#include 
#include 
class my_class
{
public:
    int for_a_long_time(int n)//s
    {
        std::cout<<"for_a_long_time:begin"<<std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(n));
        std::cout<<"for_a_long_time:end"<<std::endl;
        return 0;
    }
};


int main()
{
    my_class my_object1;
    std::future<int> f1=std::async(std::launch::async,&my_class::for_a_long_time, my_object1,5);//s 异步
    std::cout << "over1"<< std::endl;
    return 0;
}

输出

over1
for_a_long_time:begin
for_a_long_time:end

异步变同步,缩短std::async返回值的生命周期

#include 
#include 
#include 
class my_class
{
public:
    int for_a_long_time(int n)//s
    {
        std::cout<<"for_a_long_time:begin"<<std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(n));
        std::cout<<"for_a_long_time:end"<<std::endl;
        return 0;
    }
};


int main()
{
    my_class my_object1;
    std::async(std::launch::async,&my_class::for_a_long_time, my_object1,5);//s 同步
    std::cout << "over1"<< std::endl;
    return 0;
}

输出

for_a_long_time:begin
for_a_long_time:end
over1

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/717001.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)