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