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