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 | // ~packaged_task(); |
| 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 {return data_ + i + j;} |
| 29 | }; |
| 30 | |
Howard Hinnant | 932209b | 2011-05-17 23:32:48 +0000 | [diff] [blame] | 31 | void func(std::packaged_task<double(int, char)> p) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 32 | { |
| 33 | } |
| 34 | |
Howard Hinnant | 932209b | 2011-05-17 23:32:48 +0000 | [diff] [blame] | 35 | void func2(std::packaged_task<double(int, char)> p) |
Howard Hinnant | 54da338 | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 36 | { |
| 37 | p(3, 'a'); |
| 38 | } |
| 39 | |
| 40 | int main() |
| 41 | { |
| 42 | { |
| 43 | std::packaged_task<double(int, char)> p(A(5)); |
| 44 | std::future<double> f = p.get_future(); |
| 45 | std::thread(func, std::move(p)).detach(); |
| 46 | try |
| 47 | { |
| 48 | double i = f.get(); |
| 49 | assert(false); |
| 50 | } |
| 51 | catch (const std::future_error& e) |
| 52 | { |
| 53 | assert(e.code() == make_error_code(std::future_errc::broken_promise)); |
| 54 | } |
| 55 | } |
| 56 | { |
| 57 | std::packaged_task<double(int, char)> p(A(5)); |
| 58 | std::future<double> f = p.get_future(); |
| 59 | std::thread(func2, std::move(p)).detach(); |
| 60 | assert(f.get() == 105.0); |
| 61 | } |
| 62 | } |