64,652
社区成员
发帖
与我相关
我的任务
分享
#include <list>
#include <thread>
using namespace std;
struct Data
{
int from;
int to;
long long sum;
};
Data sum(int from, int to)
{
Data s;
while(true)
{
s.from = from;
s.to = to;
s.sum = 0;
for(int i = from; i <= to; i++)
{
s.sum += i;
}
for(int i = to; i >= (to+from)/2; i--)
{
s.sum -= i;
}
}
//printf("%d+...+%d-%d...-%d=%lld\n", s.from, s.to, s.to, (s.to+from)/2, s.sum);
//this_thread::sleep_for(std::chrono::milliseconds(50));
return s;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::future<Data> retData = std::async(std::bind(&sum, 20, 100000));
while(!retData.valid())
{
continue;
}
cout<<"Data Reday"<<endl;
Data r = retData.get();
cout<<"from:"<<r.from<<" to:"<<r.to<<" result:"<<r.sum<<endl;
return 0;
}