Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +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 | 99be823 | 2010-09-03 18:39:25 +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 | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 11 | |
| 12 | // <future> |
| 13 | |
| 14 | // class shared_future<R> |
| 15 | |
| 16 | // void wait() const; |
| 17 | |
| 18 | #include <future> |
| 19 | #include <cassert> |
| 20 | |
Howard Hinnant | 932209b | 2011-05-17 23:32:48 +0000 | [diff] [blame] | 21 | void func1(std::promise<int> p) |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 22 | { |
| 23 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 24 | p.set_value(3); |
| 25 | } |
| 26 | |
| 27 | int j = 0; |
| 28 | |
Howard Hinnant | 932209b | 2011-05-17 23:32:48 +0000 | [diff] [blame] | 29 | void func3(std::promise<int&> p) |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 30 | { |
| 31 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 32 | j = 5; |
| 33 | p.set_value(j); |
| 34 | } |
| 35 | |
Howard Hinnant | 932209b | 2011-05-17 23:32:48 +0000 | [diff] [blame] | 36 | void func5(std::promise<void> p) |
Howard Hinnant | 99be823 | 2010-09-03 18:39:25 +0000 | [diff] [blame] | 37 | { |
| 38 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 39 | p.set_value(); |
| 40 | } |
| 41 | |
| 42 | int main() |
| 43 | { |
| 44 | typedef std::chrono::high_resolution_clock Clock; |
| 45 | typedef std::chrono::duration<double, std::milli> ms; |
| 46 | { |
| 47 | typedef int T; |
| 48 | std::promise<T> p; |
| 49 | std::shared_future<T> f = p.get_future(); |
| 50 | std::thread(func1, std::move(p)).detach(); |
| 51 | assert(f.valid()); |
| 52 | f.wait(); |
| 53 | assert(f.valid()); |
| 54 | Clock::time_point t0 = Clock::now(); |
| 55 | f.wait(); |
| 56 | Clock::time_point t1 = Clock::now(); |
| 57 | assert(f.valid()); |
| 58 | assert(t1-t0 < ms(5)); |
| 59 | } |
| 60 | { |
| 61 | typedef int& T; |
| 62 | std::promise<T> p; |
| 63 | std::shared_future<T> f = p.get_future(); |
| 64 | std::thread(func3, std::move(p)).detach(); |
| 65 | assert(f.valid()); |
| 66 | f.wait(); |
| 67 | assert(f.valid()); |
| 68 | Clock::time_point t0 = Clock::now(); |
| 69 | f.wait(); |
| 70 | Clock::time_point t1 = Clock::now(); |
| 71 | assert(f.valid()); |
| 72 | assert(t1-t0 < ms(5)); |
| 73 | } |
| 74 | { |
| 75 | typedef void T; |
| 76 | std::promise<T> p; |
| 77 | std::shared_future<T> f = p.get_future(); |
| 78 | std::thread(func5, std::move(p)).detach(); |
| 79 | assert(f.valid()); |
| 80 | f.wait(); |
| 81 | assert(f.valid()); |
| 82 | Clock::time_point t0 = Clock::now(); |
| 83 | f.wait(); |
| 84 | Clock::time_point t1 = Clock::now(); |
| 85 | assert(f.valid()); |
| 86 | assert(t1-t0 < ms(5)); |
| 87 | } |
| 88 | } |