1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // XFAIL: libcpp-no-exceptions 11 // UNSUPPORTED: libcpp-has-no-threads 12 // UNSUPPORTED: c++98, c++03 13 14 // <future> 15 16 // class packaged_task<R(ArgTypes...)> 17 18 // void operator()(ArgTypes... args); 19 20 #include <future> 21 #include <cassert> 22 23 class A 24 { 25 long data_; 26 27 public: 28 explicit A(long i) : data_(i) {} 29 30 long operator()(long i, long j) const 31 { 32 if (j == 'z') 33 throw A(6); 34 return data_ + i + j; 35 } 36 }; 37 38 void func0(std::packaged_task<double(int, char)> p) 39 { 40 std::this_thread::sleep_for(std::chrono::milliseconds(500)); 41 p(3, 'a'); 42 } 43 44 void func1(std::packaged_task<double(int, char)> p) 45 { 46 std::this_thread::sleep_for(std::chrono::milliseconds(500)); 47 p(3, 'z'); 48 } 49 50 void func2(std::packaged_task<double(int, char)> p) 51 { 52 p(3, 'a'); 53 try 54 { 55 p(3, 'c'); 56 } 57 catch (const std::future_error& e) 58 { 59 assert(e.code() == make_error_code(std::future_errc::promise_already_satisfied)); 60 } 61 } 62 63 void func3(std::packaged_task<double(int, char)> p) 64 { 65 try 66 { 67 p(3, 'a'); 68 } 69 catch (const std::future_error& e) 70 { 71 assert(e.code() == make_error_code(std::future_errc::no_state)); 72 } 73 } 74 75 int main() 76 { 77 { 78 std::packaged_task<double(int, char)> p(A(5)); 79 std::future<double> f = p.get_future(); 80 std::thread(func0, std::move(p)).detach(); 81 assert(f.get() == 105.0); 82 } 83 { 84 std::packaged_task<double(int, char)> p(A(5)); 85 std::future<double> f = p.get_future(); 86 std::thread(func1, std::move(p)).detach(); 87 try 88 { 89 f.get(); 90 assert(false); 91 } 92 catch (const A& e) 93 { 94 assert(e(3, 'a') == 106); 95 } 96 } 97 { 98 std::packaged_task<double(int, char)> p(A(5)); 99 std::future<double> f = p.get_future(); 100 std::thread t(func2, std::move(p)); 101 assert(f.get() == 105.0); 102 t.join(); 103 } 104 { 105 std::packaged_task<double(int, char)> p; 106 std::thread t(func3, std::move(p)); 107 t.join(); 108 } 109 } 110