Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <condition_variable.hpp>
  4.  
  5. #include <cstddef>
  6. #include <mutex>
  7.  
  8. namespace tpcc {
  9. namespace solutions {
  10.  
  11. class CyclicBarrier {
  12. public:
  13. explicit CyclicBarrier(const size_t num_threads)
  14. : num_threads_(num_threads), counts{0, 0}, index_(0) {
  15. }
  16.  
  17. void PassThrough() {
  18.  
  19. std::unique_lock<std::mutex> lock{mutex_};
  20.  
  21. size_t current_index = index_;
  22. ++counts[current_index];
  23. if (counts[current_index] < num_threads_) {
  24. while (counts[current_index] < num_threads_) {
  25. cv_.wait(lock);
  26. }
  27. } else {
  28. index_ ^= 1;
  29. counts[index_] = 0;
  30. cv_.notify_all();
  31. }
  32. }
  33.  
  34. private:
  35. std::mutex mutex_;
  36. tpcc::condition_variable cv_;
  37. size_t num_threads_;
  38. size_t counts[2];
  39. size_t index_;
  40. };
  41.  
  42. } // namespace solutions
  43. } // namespace tpcc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement