Home | History | Annotate | Download | only in thread.condition
      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 // notify_all_at_thread_exit(...) requires move semantics to transfer the
     13 // unique_lock.
     14 // UNSUPPORTED: c++98, c++03
     15 
     16 // <condition_variable>
     17 
     18 // void
     19 //   notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
     20 
     21 #include <condition_variable>
     22 #include <mutex>
     23 #include <thread>
     24 #include <chrono>
     25 #include <cassert>
     26 
     27 std::condition_variable cv;
     28 std::mutex mut;
     29 
     30 typedef std::chrono::milliseconds ms;
     31 typedef std::chrono::high_resolution_clock Clock;
     32 
     33 void func()
     34 {
     35     std::unique_lock<std::mutex> lk(mut);
     36     std::notify_all_at_thread_exit(cv, std::move(lk));
     37     std::this_thread::sleep_for(ms(300));
     38 }
     39 
     40 int main()
     41 {
     42     std::unique_lock<std::mutex> lk(mut);
     43     std::thread t(func);
     44     Clock::time_point t0 = Clock::now();
     45     cv.wait(lk);
     46     Clock::time_point t1 = Clock::now();
     47     assert(t1-t0 > ms(250));
     48     t.join();
     49 }
     50