c++ cyclic barrier
Certainly! Here's an example of a cyclic barrier in C++ with explanations for each step:
#include <iostream>
#include <mutex>
#include <condition_variable>
class CyclicBarrier {
public:
explicit CyclicBarrier(int parties) : parties_(parties), count_(parties), generation_(0) {}
void await() {
std::unique_lock<std::mutex> lock(mutex_);
int gen = generation_;
if (--count_ == 0) {
// Last thread to arrive, reset count for next generation
count_ = parties_;
++generation_;
cv_.notify_all();
} else {
// Wait until the generation changes
cv_.wait(lock, [this, gen] { return gen != generation_; });
}
}
private:
std::mutex mutex_;
std::condition_variable cv_;
int parties_;
int count_;
int generation_;
};
// Example usage
void worker(int id, CyclicBarrier& barrier) {
std::cout << "Worker " << id << " started" << std::endl;
// Simulate work
// ...
barrier.await();
std::cout << "Worker " << id << " finished" << std::endl;
}
int main() {
const int num_threads = 4;
CyclicBarrier barrier(num_threads);
// Start worker threads
std::thread threads[num_threads];
for (int i = 0; i < num_threads; ++i) {
threads[i] = std::thread(worker, i, std::ref(barrier));
}
// Join threads
for (int i = 0; i < num_threads; ++i) {
threads[i].join();
}
return 0;
}
Explanation:
- CyclicBarrier Class: Implements a cyclic barrier synchronization primitive.
- Constructor: Initializes the barrier with the given number of parties (threads).
- await(): Method called by each thread to wait at the barrier.
- Mutex and Condition Variable: Used for thread synchronization and signaling.
- Parties, Count, and Generation: Keep track of the number of parties, the count of arrived threads, and the barrier generation.
- await() Method Explanation:
- Acquires a lock.
- Checks the current generation and decrements the count of arrived threads.
- If the current thread is the last one to arrive, it resets the count for the next generation and notifies all waiting threads.
- Otherwise, it waits until the generation changes using the condition variable.
- Worker Function: Simulates work and waits at the barrier.
- Main Function:
- Creates a
CyclicBarrier
object and multiple worker threads. - Starts the worker threads and waits for them to complete by joining them.
This implementation demonstrates how the cyclic barrier allows multiple threads to synchronize at a predefined point in the code.