//===----------------------------------------------------------------------===// // // The LLVM Compiler Infrastructure // // This file is dual licensed under the MIT and the University of Illinois Open // Source Licenses. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // template // future::type> // async(F&& f, Args&&... args); // template // future::type> // async(launch policy, F&& f, Args&&... args); #include #include #include typedef std::chrono::high_resolution_clock Clock; typedef std::chrono::milliseconds ms; int f0() { std::this_thread::sleep_for(ms(200)); return 3; } int i = 0; int& f1() { std::this_thread::sleep_for(ms(200)); return i; } void f2() { std::this_thread::sleep_for(ms(200)); } std::unique_ptr f3(int i) { std::this_thread::sleep_for(ms(200)); return std::unique_ptr(new int(i)); } std::unique_ptr f4(std::unique_ptr&& p) { std::this_thread::sleep_for(ms(200)); return std::move(p); } int main() { { std::future f = std::async(f0); std::this_thread::sleep_for(ms(300)); Clock::time_point t0 = Clock::now(); assert(f.get() == 3); Clock::time_point t1 = Clock::now(); assert(t1-t0 < ms(100)); } { std::future f = std::async(std::launch::async, f0); std::this_thread::sleep_for(ms(300)); Clock::time_point t0 = Clock::now(); assert(f.get() == 3); Clock::time_point t1 = Clock::now(); assert(t1-t0 < ms(100)); } { std::future f = std::async(std::launch::any, f0); std::this_thread::sleep_for(ms(300)); Clock::time_point t0 = Clock::now(); assert(f.get() == 3); Clock::time_point t1 = Clock::now(); assert(t1-t0 < ms(100)); } { std::future f = std::async(std::launch::deferred, f0); std::this_thread::sleep_for(ms(300)); Clock::time_point t0 = Clock::now(); assert(f.get() == 3); Clock::time_point t1 = Clock::now(); assert(t1-t0 > ms(100)); } { std::future f = std::async(f1); std::this_thread::sleep_for(ms(300)); Clock::time_point t0 = Clock::now(); assert(&f.get() == &i); Clock::time_point t1 = Clock::now(); assert(t1-t0 < ms(100)); } { std::future f = std::async(std::launch::async, f1); std::this_thread::sleep_for(ms(300)); Clock::time_point t0 = Clock::now(); assert(&f.get() == &i); Clock::time_point t1 = Clock::now(); assert(t1-t0 < ms(100)); } { std::future f = std::async(std::launch::any, f1); std::this_thread::sleep_for(ms(300)); Clock::time_point t0 = Clock::now(); assert(&f.get() == &i); Clock::time_point t1 = Clock::now(); assert(t1-t0 < ms(100)); } { std::future f = std::async(std::launch::deferred, f1); std::this_thread::sleep_for(ms(300)); Clock::time_point t0 = Clock::now(); assert(&f.get() == &i); Clock::time_point t1 = Clock::now(); assert(t1-t0 > ms(100)); } { std::future f = std::async(f2); std::this_thread::sleep_for(ms(300)); Clock::time_point t0 = Clock::now(); f.get(); Clock::time_point t1 = Clock::now(); assert(t1-t0 < ms(100)); } { std::future f = std::async(std::launch::async, f2); std::this_thread::sleep_for(ms(300)); Clock::time_point t0 = Clock::now(); f.get(); Clock::time_point t1 = Clock::now(); assert(t1-t0 < ms(100)); } { std::future f = std::async(std::launch::any, f2); std::this_thread::sleep_for(ms(300)); Clock::time_point t0 = Clock::now(); f.get(); Clock::time_point t1 = Clock::now(); assert(t1-t0 < ms(100)); } { std::future f = std::async(std::launch::deferred, f2); std::this_thread::sleep_for(ms(300)); Clock::time_point t0 = Clock::now(); f.get(); Clock::time_point t1 = Clock::now(); assert(t1-t0 > ms(100)); } { std::future> f = std::async(f3, 3); std::this_thread::sleep_for(ms(300)); Clock::time_point t0 = Clock::now(); assert(*f.get() == 3); Clock::time_point t1 = Clock::now(); assert(t1-t0 < ms(100)); } { std::future> f = std::async(f4, std::unique_ptr(new int(3))); std::this_thread::sleep_for(ms(300)); Clock::time_point t0 = Clock::now(); assert(*f.get() == 3); Clock::time_point t1 = Clock::now(); assert(t1-t0 < ms(100)); } }