lock.pass.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. // <mutex>
  11. // class recursive_mutex;
  12. // void lock();
  13. #include <mutex>
  14. #include <thread>
  15. #include <cstdlib>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. std::recursive_mutex m;
  19. typedef std::chrono::system_clock Clock;
  20. typedef Clock::time_point time_point;
  21. typedef Clock::duration duration;
  22. typedef std::chrono::milliseconds ms;
  23. typedef std::chrono::nanoseconds ns;
  24. void f()
  25. {
  26. time_point t0 = Clock::now();
  27. m.lock();
  28. time_point t1 = Clock::now();
  29. m.lock();
  30. m.unlock();
  31. m.unlock();
  32. ns d = t1 - t0 - ms(250);
  33. assert(d < ms(200)); // within 200ms
  34. }
  35. int main(int, char**)
  36. {
  37. m.lock();
  38. std::thread t(f);
  39. std::this_thread::sleep_for(ms(250));
  40. m.unlock();
  41. t.join();
  42. return 0;
  43. }