destructor.pass.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. // ~condition_variable();
  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 m;
  20. typedef std::unique_lock<std::mutex> Lock;
  21. bool f_ready = false;
  22. bool g_ready = false;
  23. void f()
  24. {
  25. Lock lk(m);
  26. f_ready = true;
  27. cv->notify_one();
  28. delete cv;
  29. }
  30. void g()
  31. {
  32. Lock lk(m);
  33. g_ready = true;
  34. cv->notify_one();
  35. while (!f_ready)
  36. cv->wait(lk);
  37. }
  38. int main(int, char**)
  39. {
  40. cv = new std::condition_variable;
  41. std::thread th2(g);
  42. Lock lk(m);
  43. while (!g_ready)
  44. cv->wait(lk);
  45. lk.unlock();
  46. std::thread th1(f);
  47. th1.join();
  48. th2.join();
  49. return 0;
  50. }