Home | History | Annotate | Download | only in thread.condition.condvarany
      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 
     12 // <condition_variable>
     13 
     14 // class condition_variable_any;
     15 
     16 // template <class Lock, class Rep, class Period, class Predicate>
     17 //   bool
     18 //   wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time,
     19 //            Predicate pred);
     20 
     21 #include <condition_variable>
     22 #include <mutex>
     23 #include <thread>
     24 #include <chrono>
     25 #include <cassert>
     26 
     27 class Pred
     28 {
     29     int& i_;
     30 public:
     31     explicit Pred(int& i) : i_(i) {}
     32 
     33     bool operator()() {return i_ != 0;}
     34 };
     35 
     36 std::condition_variable_any cv;
     37 
     38 typedef std::timed_mutex L0;
     39 typedef std::unique_lock<L0> L1;
     40 
     41 L0 m0;
     42 
     43 int test1 = 0;
     44 int test2 = 0;
     45 
     46 int runs = 0;
     47 
     48 void f()
     49 {
     50     typedef std::chrono::system_clock Clock;
     51     typedef std::chrono::milliseconds milliseconds;
     52     L1 lk(m0);
     53     assert(test2 == 0);
     54     test1 = 1;
     55     cv.notify_one();
     56     Clock::time_point t0 = Clock::now();
     57     bool r = cv.wait_for(lk, milliseconds(250), Pred(test2));
     58     Clock::time_point t1 = Clock::now();
     59     if (runs == 0)
     60     {
     61         assert(t1 - t0 < milliseconds(250));
     62         assert(test2 != 0);
     63     }
     64     else
     65     {
     66         assert(t1 - t0 - milliseconds(250) < milliseconds(50));
     67         assert(test2 == 0);
     68     }
     69     ++runs;
     70 }
     71 
     72 int main()
     73 {
     74     {
     75         L1 lk(m0);
     76         std::thread t(f);
     77         assert(test1 == 0);
     78         while (test1 == 0)
     79             cv.wait(lk);
     80         assert(test1 != 0);
     81         test2 = 1;
     82         lk.unlock();
     83         cv.notify_one();
     84         t.join();
     85     }
     86     test1 = 0;
     87     test2 = 0;
     88     {
     89         L1 lk(m0);
     90         std::thread t(f);
     91         assert(test1 == 0);
     92         while (test1 == 0)
     93             cv.wait(lk);
     94         assert(test1 != 0);
     95         lk.unlock();
     96         t.join();
     97     }
     98 }
     99