Home > Back-end >  About thread return value STD: : future questions
About thread return value STD: : future questions

Time:11-20

 
# include & lt; List>
# include & lt; Thread>

using namespace std;
Struct Data
{
Int the from;
int to;
Long long sum;
};
Data sum (int the from, int to)
{
The Data s;
While (true)
{
S. ROM=the from;
S.t o=to;
S.s um=0;
For (int I=the from; i <=to; I++)
{
S.s um +=I;
}
For (int I=to; I & gt;=(to a + from)/2; I -)
{
S.s um -=I;
}
}

//printf (" % d +... + % d % d... - % d=% LLD \ n ", s. ROM, s.t o, s.t o, (s.t o + from)/2, s.s um);
//this_thread: : sleep_for (STD: : chrono: : milliseconds (50));
return s;
}


Int main (int arg c, char * argv [])
{
QCoreApplication a (arg c, argv);

STD: : future RetData=https://bbs.csdn.net/topics/std::async (STD: : bind (& sum, 20, 100000));
while(! RetData. Valid ())
{
continue;
}
Cout<" Data Reday "& lt; Data r=retData. The get ();
Cout<" From: "& lt; return 0;
}

Himself in the use of the future, I found there is something wrong with the valid value of the future, when I have an infinite loop, this code results check value valid returns true, cause in the console to play out, and then get places, etc., this is obviously wrong,
When I was in the use of thread pool found this problem, when I was in the main thread to check the future, I think that value is ready, result in equivalent, didn't do other things to do, please explain a great god, the wrong place and correct use thank you
I use the IDE: 5.9.3 + vc2015 x86
  • Related