lock.pass.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 timed_mutex;
  12. // void lock();
  13. #include <mutex>
  14. #include <thread>
  15. #include <cstdlib>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. std::timed_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.unlock();
  30. ns d = t1 - t0 - ms(250);
  31. assert(d < ms(50)); // within 50ms
  32. }
  33. int main(int, char**)
  34. {
  35. m.lock();
  36. std::thread t(f);
  37. std::this_thread::sleep_for(ms(250));
  38. m.unlock();
  39. t.join();
  40. return 0;
  41. }