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