test_11499.cpp 1009 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright (C) 2014 Vicente Botet
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #define BOOST_THREAD_VERSION 4
  6. #include <boost/thread/shared_mutex.hpp>
  7. #include <thread>
  8. #include <mutex>
  9. #include <shared_mutex>
  10. #include <atomic>
  11. #include <vector>
  12. using MutexT = boost::shared_mutex;
  13. using ReaderLockT = std::lock_guard<MutexT>;
  14. using WriterLockT = std::shared_lock<MutexT>;
  15. MutexT gMutex;
  16. std::atomic<bool> running(true);
  17. void myread()
  18. {
  19. long reads = 0;
  20. while (running && reads < 100000)
  21. {
  22. ReaderLockT lock(gMutex);
  23. std::this_thread::yield();
  24. ++reads;
  25. }
  26. }
  27. int main()
  28. {
  29. using namespace std;
  30. vector<thread> threads;
  31. for (int i = 0; i < 256; ++i)
  32. {
  33. threads.emplace_back(thread(myread));
  34. }
  35. // string str;
  36. //
  37. // getline(std::cin, str);
  38. running = false;
  39. for (auto& thread : threads)
  40. {
  41. thread.join();
  42. }
  43. return 0;
  44. }