Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <condition_variable.hpp>
- #include <cstddef>
- #include <mutex>
- namespace tpcc {
- namespace solutions {
- class CyclicBarrier {
- public:
- explicit CyclicBarrier(const size_t num_threads)
- : num_threads_(num_threads), counts{0, 0}, index_(0) {
- }
- void PassThrough() {
- std::unique_lock<std::mutex> lock{mutex_};
- size_t current_index = index_;
- ++counts[current_index];
- if (counts[current_index] < num_threads_) {
- while (counts[current_index] < num_threads_) {
- cv_.wait(lock);
- }
- } else {
- index_ ^= 1;
- counts[index_] = 0;
- cv_.notify_all();
- }
- }
- private:
- std::mutex mutex_;
- tpcc::condition_variable cv_;
- size_t num_threads_;
- size_t counts[2];
- size_t index_;
- };
- } // namespace solutions
- } // namespace tpcc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement