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 // <future>
     11 
     12 // class future<R>
     13 
     14 // template <class Rep, class Period>
     15 //   future_status
     16 //   wait_for(const chrono::duration<Rep, Period>& rel_time) const;
     17 
     18 #include <future>
     19 #include <cassert>
     20 
     21 typedef std::chrono::milliseconds ms;
     22 
     23 void func1(std::promise<int> p)
     24 {
     25     std::this_thread::sleep_for(ms(500));
     26     p.set_value(3);
     27 }
     28 
     29 int j = 0;
     30 
     31 void func3(std::promise<int&> p)
     32 {
     33     std::this_thread::sleep_for(ms(500));
     34     j = 5;
     35     p.set_value(j);
     36 }
     37 
     38 void func5(std::promise<void> p)
     39 {
     40     std::this_thread::sleep_for(ms(500));
     41     p.set_value();
     42 }
     43 
     44 int main()
     45 {
     46     typedef std::chrono::high_resolution_clock Clock;
     47     {
     48         typedef int T;
     49         std::promise<T> p;
     50         std::future<T> f = p.get_future();
     51         std::thread(func1, std::move(p)).detach();
     52         assert(f.valid());
     53         assert(f.wait_for(ms(300)) == std::future_status::timeout);
     54         assert(f.valid());
     55         assert(f.wait_for(ms(300)) == std::future_status::ready);
     56         assert(f.valid());
     57         Clock::time_point t0 = Clock::now();
     58         f.wait();
     59         Clock::time_point t1 = Clock::now();
     60         assert(f.valid());
     61         assert(t1-t0 < ms(50));
     62     }
     63     {
     64         typedef int& T;
     65         std::promise<T> p;
     66         std::future<T> f = p.get_future();
     67         std::thread(func3, std::move(p)).detach();
     68         assert(f.valid());
     69         assert(f.wait_for(ms(300)) == std::future_status::timeout);
     70         assert(f.valid());
     71         assert(f.wait_for(ms(300)) == std::future_status::ready);
     72         assert(f.valid());
     73         Clock::time_point t0 = Clock::now();
     74         f.wait();
     75         Clock::time_point t1 = Clock::now();
     76         assert(f.valid());
     77         assert(t1-t0 < ms(50));
     78     }
     79     {
     80         typedef void T;
     81         std::promise<T> p;
     82         std::future<T> f = p.get_future();
     83         std::thread(func5, std::move(p)).detach();
     84         assert(f.valid());
     85         assert(f.wait_for(ms(300)) == std::future_status::timeout);
     86         assert(f.valid());
     87         assert(f.wait_for(ms(300)) == std::future_status::ready);
     88         assert(f.valid());
     89         Clock::time_point t0 = Clock::now();
     90         f.wait();
     91         Clock::time_point t1 = Clock::now();
     92         assert(f.valid());
     93         assert(t1-t0 < ms(50));
     94     }
     95 }
     96