1*4684ddb6SLionel Sambuc //===----------------------------------------------------------------------===// 2*4684ddb6SLionel Sambuc // 3*4684ddb6SLionel Sambuc // The LLVM Compiler Infrastructure 4*4684ddb6SLionel Sambuc // 5*4684ddb6SLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open 6*4684ddb6SLionel Sambuc // Source Licenses. See LICENSE.TXT for details. 7*4684ddb6SLionel Sambuc // 8*4684ddb6SLionel Sambuc //===----------------------------------------------------------------------===// 9*4684ddb6SLionel Sambuc 10*4684ddb6SLionel Sambuc // <future> 11*4684ddb6SLionel Sambuc 12*4684ddb6SLionel Sambuc // class future<R> 13*4684ddb6SLionel Sambuc 14*4684ddb6SLionel Sambuc // void wait() const; 15*4684ddb6SLionel Sambuc 16*4684ddb6SLionel Sambuc #include <future> 17*4684ddb6SLionel Sambuc #include <cassert> 18*4684ddb6SLionel Sambuc func1(std::promise<int> p)19*4684ddb6SLionel Sambucvoid func1(std::promise<int> p) 20*4684ddb6SLionel Sambuc { 21*4684ddb6SLionel Sambuc std::this_thread::sleep_for(std::chrono::milliseconds(500)); 22*4684ddb6SLionel Sambuc p.set_value(3); 23*4684ddb6SLionel Sambuc } 24*4684ddb6SLionel Sambuc 25*4684ddb6SLionel Sambuc int j = 0; 26*4684ddb6SLionel Sambuc func3(std::promise<int &> p)27*4684ddb6SLionel Sambucvoid func3(std::promise<int&> p) 28*4684ddb6SLionel Sambuc { 29*4684ddb6SLionel Sambuc std::this_thread::sleep_for(std::chrono::milliseconds(500)); 30*4684ddb6SLionel Sambuc j = 5; 31*4684ddb6SLionel Sambuc p.set_value(j); 32*4684ddb6SLionel Sambuc } 33*4684ddb6SLionel Sambuc func5(std::promise<void> p)34*4684ddb6SLionel Sambucvoid func5(std::promise<void> p) 35*4684ddb6SLionel Sambuc { 36*4684ddb6SLionel Sambuc std::this_thread::sleep_for(std::chrono::milliseconds(500)); 37*4684ddb6SLionel Sambuc p.set_value(); 38*4684ddb6SLionel Sambuc } 39*4684ddb6SLionel Sambuc main()40*4684ddb6SLionel Sambucint main() 41*4684ddb6SLionel Sambuc { 42*4684ddb6SLionel Sambuc typedef std::chrono::high_resolution_clock Clock; 43*4684ddb6SLionel Sambuc typedef std::chrono::duration<double, std::milli> ms; 44*4684ddb6SLionel Sambuc { 45*4684ddb6SLionel Sambuc typedef int T; 46*4684ddb6SLionel Sambuc std::promise<T> p; 47*4684ddb6SLionel Sambuc std::future<T> f = p.get_future(); 48*4684ddb6SLionel Sambuc std::thread(func1, std::move(p)).detach(); 49*4684ddb6SLionel Sambuc assert(f.valid()); 50*4684ddb6SLionel Sambuc f.wait(); 51*4684ddb6SLionel Sambuc assert(f.valid()); 52*4684ddb6SLionel Sambuc Clock::time_point t0 = Clock::now(); 53*4684ddb6SLionel Sambuc f.wait(); 54*4684ddb6SLionel Sambuc Clock::time_point t1 = Clock::now(); 55*4684ddb6SLionel Sambuc assert(f.valid()); 56*4684ddb6SLionel Sambuc assert(t1-t0 < ms(5)); 57*4684ddb6SLionel Sambuc } 58*4684ddb6SLionel Sambuc { 59*4684ddb6SLionel Sambuc typedef int& T; 60*4684ddb6SLionel Sambuc std::promise<T> p; 61*4684ddb6SLionel Sambuc std::future<T> f = p.get_future(); 62*4684ddb6SLionel Sambuc std::thread(func3, std::move(p)).detach(); 63*4684ddb6SLionel Sambuc assert(f.valid()); 64*4684ddb6SLionel Sambuc f.wait(); 65*4684ddb6SLionel Sambuc assert(f.valid()); 66*4684ddb6SLionel Sambuc Clock::time_point t0 = Clock::now(); 67*4684ddb6SLionel Sambuc f.wait(); 68*4684ddb6SLionel Sambuc Clock::time_point t1 = Clock::now(); 69*4684ddb6SLionel Sambuc assert(f.valid()); 70*4684ddb6SLionel Sambuc assert(t1-t0 < ms(5)); 71*4684ddb6SLionel Sambuc } 72*4684ddb6SLionel Sambuc { 73*4684ddb6SLionel Sambuc typedef void T; 74*4684ddb6SLionel Sambuc std::promise<T> p; 75*4684ddb6SLionel Sambuc std::future<T> f = p.get_future(); 76*4684ddb6SLionel Sambuc std::thread(func5, std::move(p)).detach(); 77*4684ddb6SLionel Sambuc assert(f.valid()); 78*4684ddb6SLionel Sambuc f.wait(); 79*4684ddb6SLionel Sambuc assert(f.valid()); 80*4684ddb6SLionel Sambuc Clock::time_point t0 = Clock::now(); 81*4684ddb6SLionel Sambuc f.wait(); 82*4684ddb6SLionel Sambuc Clock::time_point t1 = Clock::now(); 83*4684ddb6SLionel Sambuc assert(f.valid()); 84*4684ddb6SLionel Sambuc assert(t1-t0 < ms(5)); 85*4684ddb6SLionel Sambuc } 86*4684ddb6SLionel Sambuc } 87