Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +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 | 54da338 | 2010-08-30 18:46:21 +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 | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 11 | |
| 12 | // <future> |
| 13 | |
| 14 | // class packaged_task<R(ArgTypes...)> |
| 15 | |
| 16 | // void operator()(ArgTypes... args); |
| 17 | |
| 18 | #include <future> |
| 19 | #include <cassert> |
| 20 | |
| 21 | class A |
| 22 | { |
| 23 | long data_; |
| 24 | |
| 25 | public: |
| 26 | explicit A(long i) : data_(i) {} |
| 27 | |
| 28 | long operator()(long i, long j) const |
| 29 | { |
| 30 | if (j == 'z') |
| 31 | throw A(6); |
| 32 | return data_ + i + j; |
| 33 | } |
| 34 | }; |
| 35 | |
Howard Hinnant | 932209b | 2011-05-17 23:32:48 +0000 | [diff] [blame] | 36 | void func0(std::packaged_task<double(int, char)> p) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 37 | { |
| 38 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 39 | p(3, 'a'); |
| 40 | } |
| 41 | |
Howard Hinnant | 932209b | 2011-05-17 23:32:48 +0000 | [diff] [blame] | 42 | void func1(std::packaged_task<double(int, char)> p) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 43 | { |
| 44 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 45 | p(3, 'z'); |
| 46 | } |
| 47 | |
Howard Hinnant | 932209b | 2011-05-17 23:32:48 +0000 | [diff] [blame] | 48 | void func2(std::packaged_task<double(int, char)> p) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 49 | { |
| 50 | p(3, 'a'); |
| 51 | try |
| 52 | { |
| 53 | p(3, 'c'); |
| 54 | } |
| 55 | catch (const std::future_error& e) |
| 56 | { |
| 57 | assert(e.code() == make_error_code(std::future_errc::promise_already_satisfied)); |
| 58 | } |
| 59 | } |
| 60 | |
Howard Hinnant | 932209b | 2011-05-17 23:32:48 +0000 | [diff] [blame] | 61 | void func3(std::packaged_task<double(int, char)> p) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 62 | { |
| 63 | try |
| 64 | { |
| 65 | p(3, 'a'); |
| 66 | } |
| 67 | catch (const std::future_error& e) |
| 68 | { |
| 69 | assert(e.code() == make_error_code(std::future_errc::no_state)); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | int main() |
| 74 | { |
| 75 | { |
| 76 | std::packaged_task<double(int, char)> p(A(5)); |
| 77 | std::future<double> f = p.get_future(); |
| 78 | std::thread(func0, std::move(p)).detach(); |
| 79 | assert(f.get() == 105.0); |
| 80 | } |
| 81 | { |
| 82 | std::packaged_task<double(int, char)> p(A(5)); |
| 83 | std::future<double> f = p.get_future(); |
| 84 | std::thread(func1, std::move(p)).detach(); |
| 85 | try |
| 86 | { |
| 87 | f.get(); |
| 88 | assert(false); |
| 89 | } |
| 90 | catch (const A& e) |
| 91 | { |
| 92 | assert(e(3, 'a') == 106); |
| 93 | } |
| 94 | } |
| 95 | { |
| 96 | std::packaged_task<double(int, char)> p(A(5)); |
| 97 | std::future<double> f = p.get_future(); |
| 98 | std::thread t(func2, std::move(p)); |
| 99 | assert(f.get() == 105.0); |
| 100 | t.join(); |
| 101 | } |
| 102 | { |
| 103 | std::packaged_task<double(int, char)> p; |
| 104 | std::thread t(func3, std::move(p)); |
| 105 | t.join(); |
| 106 | } |
| 107 | } |