| 1 | //===----------------------------------------------------------------------===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // UNSUPPORTED: no-threads |
| 10 | // UNSUPPORTED: c++03 |
| 11 | |
| 12 | // <future> |
| 13 | |
| 14 | // class shared_future<R> |
| 15 | |
| 16 | // void wait() const; |
| 17 | |
| 18 | #include <cassert> |
| 19 | #include <chrono> |
| 20 | #include <future> |
| 21 | #include <ratio> |
| 22 | |
| 23 | #include "make_test_thread.h" |
| 24 | #include "test_macros.h" |
| 25 | |
| 26 | void func1(std::promise<int> p) |
| 27 | { |
| 28 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 29 | p.set_value(3); |
| 30 | } |
| 31 | |
| 32 | int j = 0; |
| 33 | |
| 34 | void func3(std::promise<int&> p) |
| 35 | { |
| 36 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 37 | j = 5; |
| 38 | p.set_value(j); |
| 39 | } |
| 40 | |
| 41 | void func5(std::promise<void> p) |
| 42 | { |
| 43 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 44 | p.set_value(); |
| 45 | } |
| 46 | |
| 47 | int main(int, char**) |
| 48 | { |
| 49 | typedef std::chrono::high_resolution_clock Clock; |
| 50 | typedef std::chrono::duration<double, std::milli> ms; |
| 51 | { |
| 52 | typedef int T; |
| 53 | std::promise<T> p; |
| 54 | std::shared_future<T> f = p.get_future(); |
| 55 | support::make_test_thread(func1, std::move(p)).detach(); |
| 56 | assert(f.valid()); |
| 57 | f.wait(); |
| 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()); |
| 63 | assert(t1-t0 < ms(5)); |
| 64 | } |
| 65 | { |
| 66 | typedef int& T; |
| 67 | std::promise<T> p; |
| 68 | std::shared_future<T> f = p.get_future(); |
| 69 | support::make_test_thread(func3, std::move(p)).detach(); |
| 70 | assert(f.valid()); |
| 71 | f.wait(); |
| 72 | assert(f.valid()); |
| 73 | Clock::time_point t0 = Clock::now(); |
| 74 | f.wait(); |
| 75 | Clock::time_point t1 = Clock::now(); |
| 76 | assert(f.valid()); |
| 77 | assert(t1-t0 < ms(5)); |
| 78 | } |
| 79 | { |
| 80 | typedef void T; |
| 81 | std::promise<T> p; |
| 82 | std::shared_future<T> f = p.get_future(); |
| 83 | support::make_test_thread(func5, std::move(p)).detach(); |
| 84 | assert(f.valid()); |
| 85 | f.wait(); |
| 86 | assert(f.valid()); |
| 87 | Clock::time_point t0 = Clock::now(); |
| 88 | f.wait(); |
| 89 | Clock::time_point t1 = Clock::now(); |
| 90 | assert(f.valid()); |
| 91 | assert(t1-t0 < ms(5)); |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 |
