Home | History | Annotate | Download | only in futures.unique_future
      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 // UNSUPPORTED: libcpp-has-no-threads
     11 // UNSUPPORTED: c++98, c++03
     12 
     13 // <future>
     14 
     15 // class future<R>
     16 
     17 // R future::get();
     18 // R& future<R&>::get();
     19 // void future<void>::get();
     20 
     21 #include <future>
     22 #include <cassert>
     23 
     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 void func2(std::promise<int> p)
     33 {
     34     std::this_thread::sleep_for(std::chrono::milliseconds(500));
     35     p.set_exception(std::make_exception_ptr(3));
     36 }
     37 
     38 int j = 0;
     39 
     40 void func3(std::promise<int&> p)
     41 {
     42     std::this_thread::sleep_for(std::chrono::milliseconds(500));
     43     j = 5;
     44     p.set_value(j);
     45 }
     46 
     47 void func4(std::promise<int&> p)
     48 {
     49     std::this_thread::sleep_for(std::chrono::milliseconds(500));
     50     p.set_exception(std::make_exception_ptr(3.5));
     51 }
     52 
     53 void func5(std::promise<void> p)
     54 {
     55     std::this_thread::sleep_for(std::chrono::milliseconds(500));
     56     p.set_value();
     57 }
     58 
     59 void func6(std::promise<void> p)
     60 {
     61     std::this_thread::sleep_for(std::chrono::milliseconds(500));
     62     p.set_exception(std::make_exception_ptr('c'));
     63 }
     64 
     65 int main()
     66 {
     67     {
     68         typedef int T;
     69         {
     70             std::promise<T> p;
     71             std::future<T> f = p.get_future();
     72             std::thread(func1, std::move(p)).detach();
     73             assert(f.valid());
     74             assert(f.get() == 3);
     75             assert(!f.valid());
     76         }
     77 #ifndef TEST_HAS_NO_EXCEPTIONS
     78         {
     79             std::promise<T> p;
     80             std::future<T> f = p.get_future();
     81             std::thread(func2, std::move(p)).detach();
     82             try
     83             {
     84                 assert(f.valid());
     85                 assert(f.get() == 3);
     86                 assert(false);
     87             }
     88             catch (int i)
     89             {
     90                 assert(i == 3);
     91             }
     92             assert(!f.valid());
     93         }
     94 #endif
     95     }
     96     {
     97         typedef int& T;
     98         {
     99             std::promise<T> p;
    100             std::future<T> f = p.get_future();
    101             std::thread(func3, std::move(p)).detach();
    102             assert(f.valid());
    103             assert(f.get() == 5);
    104             assert(!f.valid());
    105         }
    106 #ifndef TEST_HAS_NO_EXCEPTIONS
    107         {
    108             std::promise<T> p;
    109             std::future<T> f = p.get_future();
    110             std::thread(func4, std::move(p)).detach();
    111             try
    112             {
    113                 assert(f.valid());
    114                 assert(f.get() == 3);
    115                 assert(false);
    116             }
    117             catch (double i)
    118             {
    119                 assert(i == 3.5);
    120             }
    121             assert(!f.valid());
    122         }
    123 #endif
    124     }
    125     {
    126         typedef void T;
    127         {
    128             std::promise<T> p;
    129             std::future<T> f = p.get_future();
    130             std::thread(func5, std::move(p)).detach();
    131             assert(f.valid());
    132             f.get();
    133             assert(!f.valid());
    134         }
    135 #ifndef TEST_HAS_NO_EXCEPTIONS
    136         {
    137             std::promise<T> p;
    138             std::future<T> f = p.get_future();
    139             std::thread(func6, std::move(p)).detach();
    140             try
    141             {
    142                 assert(f.valid());
    143                 f.get();
    144                 assert(false);
    145             }
    146             catch (char i)
    147             {
    148                 assert(i == 'c');
    149             }
    150             assert(!f.valid());
    151         }
    152 #endif
    153     }
    154 }
    155