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