wait.pass.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // UNSUPPORTED: libcpp-has-no-threads
  10. // <condition_variable>
  11. // class condition_variable;
  12. // void wait(unique_lock<mutex>& lock);
  13. #include <condition_variable>
  14. #include <mutex>
  15. #include <thread>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. std::condition_variable cv;
  19. std::mutex mut;
  20. int test1 = 0;
  21. int test2 = 0;
  22. void f()
  23. {
  24. std::unique_lock<std::mutex> lk(mut);
  25. assert(test2 == 0);
  26. test1 = 1;
  27. cv.notify_one();
  28. while (test2 == 0)
  29. cv.wait(lk);
  30. assert(test2 != 0);
  31. }
  32. int main(int, char**)
  33. {
  34. std::unique_lock<std::mutex>lk(mut);
  35. std::thread t(f);
  36. assert(test1 == 0);
  37. while (test1 == 0)
  38. cv.wait(lk);
  39. assert(test1 != 0);
  40. test2 = 1;
  41. lk.unlock();
  42. cv.notify_one();
  43. t.join();
  44. return 0;
  45. }