lock_shared.pass.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // UNSUPPORTED: c++98, c++03, c++11
  11. // FLAKY_TEST.
  12. // <shared_mutex>
  13. // class shared_timed_mutex;
  14. // void lock_shared();
  15. #include <shared_mutex>
  16. #include <thread>
  17. #include <vector>
  18. #include <cstdlib>
  19. #include <cassert>
  20. #include "test_macros.h"
  21. std::shared_timed_mutex m;
  22. typedef std::chrono::system_clock Clock;
  23. typedef Clock::time_point time_point;
  24. typedef Clock::duration duration;
  25. typedef std::chrono::milliseconds ms;
  26. typedef std::chrono::nanoseconds ns;
  27. ms WaitTime = ms(250);
  28. // Thread sanitizer causes more overhead and will sometimes cause this test
  29. // to fail. To prevent this we give Thread sanitizer more time to complete the
  30. // test.
  31. #if !defined(TEST_HAS_SANITIZERS)
  32. ms Tolerance = ms(50);
  33. #else
  34. ms Tolerance = ms(50 * 5);
  35. #endif
  36. void f()
  37. {
  38. time_point t0 = Clock::now();
  39. m.lock_shared();
  40. time_point t1 = Clock::now();
  41. m.unlock_shared();
  42. ns d = t1 - t0 - WaitTime;
  43. assert(d < Tolerance); // within tolerance
  44. }
  45. void g()
  46. {
  47. time_point t0 = Clock::now();
  48. m.lock_shared();
  49. time_point t1 = Clock::now();
  50. m.unlock_shared();
  51. ns d = t1 - t0;
  52. assert(d < Tolerance); // within tolerance
  53. }
  54. int main(int, char**)
  55. {
  56. m.lock();
  57. std::vector<std::thread> v;
  58. for (int i = 0; i < 5; ++i)
  59. v.push_back(std::thread(f));
  60. std::this_thread::sleep_for(WaitTime);
  61. m.unlock();
  62. for (auto& t : v)
  63. t.join();
  64. m.lock_shared();
  65. for (auto& t : v)
  66. t = std::thread(g);
  67. std::thread q(f);
  68. std::this_thread::sleep_for(WaitTime);
  69. m.unlock_shared();
  70. for (auto& t : v)
  71. t.join();
  72. q.join();
  73. return 0;
  74. }