C++ async异步操作
flyfish
一个简单的例子
#include <future>
#include <iostream>
#include <chrono>
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()
{
std::future<int> f = std::async(for_a_long_time, 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
lambda方式
#include <future>
#include <iostream>
#include <chrono>
int main()
{
std::future<int> f = std::async(std::launch::async, [](int n)
{
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;
}, 5);
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;
}
调用类的成员函数的方式
#include <future>
#include <iostream>
#include <chrono>
class my_class
{
public:
int for_a_long_time(int n)
{
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);
std::cout << "please wait"<<std::endl;
std::chrono::seconds span(1);
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 <future>
#include <iostream>
#include <chrono>
class my_class
{
public:
int for_a_long_time(int n)
{
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);
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 <future>
#include <iostream>
#include <chrono>
class my_class
{
public:
int for_a_long_time(int n)
{
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);
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 <future>
#include <iostream>
#include <chrono>
class my_class
{
public:
int for_a_long_time(int n)
{
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);
std::cout << "over1"<< std::endl;
return 0;
}
输出
over1
for_a_long_time:begin
for_a_long_time:end
异步变同步,缩短std::async返回值的生命周期
#include <future>
#include <iostream>
#include <chrono>
class my_class
{
public:
int for_a_long_time(int n)
{
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);
std::cout << "over1"<< std::endl;
return 0;
}
输出
for_a_long_time:begin
for_a_long_time:end
over1
|