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: libcpp-has-no-threads 10 // UNSUPPORTED: c++03 11 12 // <future> 13 14 // class packaged_task<R(ArgTypes...)> 15 16 // ~packaged_task(); 17 18 #include <future> 19 #include <cassert> 20 21 #include "make_test_thread.h" 22 #include "test_macros.h" 23 24 class A 25 { 26 long data_; 27 28 public: 29 explicit A(long i) : data_(i) {} 30 31 long operator()(long i, long j) const {return data_ + i + j;} 32 }; 33 34 void func(std::packaged_task<double(int, char)>) 35 { 36 } 37 38 void func2(std::packaged_task<double(int, char)> p) 39 { 40 p(3, 'a'); 41 } 42 43 int main(int, char**) 44 { 45 #ifndef TEST_HAS_NO_EXCEPTIONS 46 { 47 std::packaged_task<double(int, char)> p(A(5)); 48 std::future<double> f = p.get_future(); 49 support::make_test_thread(func, std::move(p)).detach(); 50 try 51 { 52 double i = f.get(); 53 ((void)i); // Prevent unused warning 54 assert(false); 55 } 56 catch (const std::future_error& e) 57 { 58 assert(e.code() == make_error_code(std::future_errc::broken_promise)); 59 } 60 } 61 #endif 62 { 63 std::packaged_task<double(int, char)> p(A(5)); 64 std::future<double> f = p.get_future(); 65 support::make_test_thread(func2, std::move(p)).detach(); 66 assert(f.get() == 105.0); 67 } 68 69 return 0; 70 } 71