#include "iostream"
#include "future"
#include "chrono"
using namespace std;
bool isPrime(int x)
{
for(int i = 2; i < x;i++)
{
if(x % i == 0)
{
return false;
}
}
return true;
}
int do_get_value()
{
return 10;
}
int main()
{
//444444443
int value = 194232491;
std::future<bool> fut = std::async(isPrime,value);
cout << "please wait....." <<endl;
std::chrono::milliseconds span(100);
//while(fut.wait_for(span) == std::future_status::timeout)
//{
// cout<< "has waited 100ms ,timeout." <<endl;
//}
fut.wait();
bool x = fut.get();
cout << value << (x?" is " : " is not ") <<" prime ."<<endl;
getchar();
return 0;
}
int main1()
{
std::future<int> fut = std::async(do_get_value);
std::shared_future<int> shFut = fut.share();
cout << " value is " << shFut.get() <<endl;
cout << " value is " << shFut.get()*2 <<endl;
}
C++11 async
最新推荐文章于 2024-07-14 10:32:40 发布