void fut_wait(){ thread::id tid = this_thread::get_id(); while(fut.valid()){ fut.wait(); bool ret = fut.get(); COUT << tid << " ret:" << ret << endl; } }
/* shared_future */ int factorial(shared_future<int> f) { // do something else int N = f.get(); // If promise is distroyed, exception: std::future_errc::broken_promise f.get(); cout << "Got from parent: " << N << endl; int res = 1; for (int i=N; i>1; i--) res *= i; return res; }
static TransData phaseCorrThr(const shared_future<FFTHolder> &fixFut, const shared_future<FFTHolder> &nbrFut, const fftwf_plan &c2rPlan, const PtPair &pair, const Point2f &absHint, const map<GridPtOff, Mat> &hintToMask, const Size &imSz) { //printf("phase %d %d, %d %d\n", pair[0][0], pair[0][1], pair[1][0], pair[1][1]); float *buf = (float *)fftwf_malloc_thr(sizeof(float) * getFFTLen(imSz)); GridPtOff off = {{pair[1][0] - pair[0][0], pair[1][1] - pair[0][1]}}; // vector from fix to nbr const Mat &mask = hintToMask.at(off); Point2f hint = absHint; hint.x *= off[0]; hint.y *= off[1]; FFTHolder fixFFT = fixFut.get(); FFTHolder nbrFFT = nbrFut.get(); /* testing Mat fix(imSz.height, imSz.width + 2, CV_32F, fixFFT.fft); Mat nbr(imSz.height, imSz.width + 2, CV_32F, nbrFFT.fft); double minVal, maxVal; Point minLoc2, maxLoc2; minMaxLoc(fix, &minVal, &maxVal, &minLoc2, &maxLoc2); printf("min: %lf, (%d, %d)\n", minVal, minLoc2.x, minLoc2.y); printf("max: %lf, (%d, %d)\n", maxVal, maxLoc2.x, maxLoc2.y); minMaxLoc(nbr, &minVal, &maxVal, &minLoc2, &maxLoc2); printf("min: %lf, (%d, %d)\n", minVal, minLoc2.x, minLoc2.y); printf("max: %lf, (%d, %d)\n", maxVal, maxLoc2.x, maxLoc2.y); imshow("a", fix); waitKey(0); */ double dist; float conf; Point maxLoc; Point bestPt = phaseCorr(fixFFT.fft, nbrFFT.fft, imSz, c2rPlan, buf, hint, dist, mask, maxLoc, conf, getConfSumNbrs); fftwf_free_thr(buf); // printf("got conf %f old conf %f rat %f, maxLoc %d %d phase %d %d, %d %d, dist %lf, mask sz: %d %d\n\n", conf, oldConf, conf / 1000 / oldConf, maxLoc.x, maxLoc.y, // pair[0][0], pair[0][1], pair[1][0], pair[1][1], dist, mask.size[1], mask.size[0]); TransData dat(bestPt.x, bestPt.y, dist, conf); //printf("done phase %d %d, %d %d\n", pair[0][0], pair[0][1], pair[1][0], pair[1][1]); return dat; }
template<typename T> inline exception_ptr get_exception_ptr(shared_future<T> &f) { #if 1 // Thanks to Vicente for adding this to Boost.Thread return f.get_exception_ptr(); #else // This seems excessive but I don't see any other legal way to extract the exception ... bool success=false; try { f.get(); success=true; } catch(...) { exception_ptr e(afio::make_exception_ptr(afio::current_exception())); assert(e); return e; } return exception_ptr(); #endif }
void doSomething(char c, shared_future<int> f){ try{ //wait fornumber of character to print int num = f.get(); //get result of queryNumber() for (int i = 0; i < num; ++i) { this_thread::sleep_for(chrono::milliseconds(100)); cout.put(c).flush(); } }catch(const exception& e){ cerr << "EXCEPTION in thread " << this_thread::get_id() << ": " << e.what() << endl; } }
void t_future(){ //NOTICE("fut.get"); //fut.get(); NOTICE("fut.wait"); fut.wait(); thread t1(fut_wait), t2(fut_wait), t3(fut_wait); while(true){ prom = promise<bool>(); fut = prom.get_future(); sleep(2); prom.set_value(false); sleep(3); } t1.join(); t2.join(); t3.join(); }
void future_void_sf1(shared_future<void> f1) { HPX_TEST(f1.is_ready()); ++future_void_f1_count; }
int promiseDependingFunction2(shared_future<int> sfu) { int value = sfu.get(); return value*2; }