#include <thread> #include <atomic> #include <condition_variable> #include <mutex>
class barrier { const unsigned int threadCount; unsigned int threadsWaiting; public: barrier(unsigned int n) : threadCount(n) { threadsWaiting = 0; } };
class barrier { const unsigned int threadCount; std::atomic<unsigned int> threadsWaiting; public: barrier(unsigned int n) : threadCount(n) { threadsWaiting = 0; } };
class barrier { const unsigned int threadCount; std::atomic<unsigned int> threadsWaiting; public: barrier(unsigned int n) : threadCount(n) { threadsWaiting = 0; } barrier(const barrier &) = delete; };
class barrier { const unsigned int threadCount; std::atomic<unsigned int> threadsWaiting; bool isNotWaiting; std::condition_variable waitVariable; std::mutex mutex; public: barrier(unsigned int n) : threadCount(n) { threadsWaiting = 0; } barrier(const barrier &) = delete; };
std::unique_lock<std::mutex> lock(mutex); waitVariable.wait(lock, [&]{ return noWait; });
isNotWaiting = true; waitVariable.notify_all(); threadsWaiting.store(0);
class barrier { const unsigned int threadCount; std::atomic<unsigned int> threadsWaiting; bool isNotWaiting; std::condition_variable waitVariable; std::mutex mutex; public: barrier(unsigned int n) : threadCount(n) { threadsWaiting = 0; } barrier(const barrier &) = delete; void wait() { if (threadsWaiting.fetch_add(1) >= threadCount - 1) { isNotWaiting = true; waitVariable.notify_all(); threadCount.store(0); } else { std::unique_lock<std::mutex> lock(mutex); waitVariable.wait(lock,[&]{ return isNoWaiting;}); } };
class barrier { const unsigned int threadCount; std::atomic<unsigned int> threadsWaiting; bool isNotWaiting; std::condition_variable waitVariable; std::mutex mutex; public: barrier(unsigned int n) : threadCount(n) { threadsWaiting = 0; isNotWaiting = false; } barrier(const barrier &) = delete; void wait() { if (threadsWaiting.fetch_add(1) >= threadCount - 1) { isNotWaiting = true; waitVariable.notify_all(); threadCount.store(0); } else { std::unique_lock<std::mutex> lock(mutex); waitVariable.wait(lock,[&]{ return isNotWaiting;}); } };
#include <iostream> #include <thread> #include <atomic> #include <condition_variable> #include <mutex> class barrier { const unsigned int threadCount; std::atomic<unsigned int>threadsWaiting; bool isNotWaiting; std::condition_variable waitVariable; std::mutex mutex; public: barrier(unsigned int n) : threadCount(n) { threadsWaiting = 0; isNotWaiting = false; } barrier(const barrier &) = delete; void wait() { if (threadsWaiting.fetch_add(1) >= threadCount - 1) { isNotWaiting = true; waitVariable.notify_all(); threadsWaiting.store(0); } else { std::unique_lock<std::mutex> lock(mutex); waitVariable.wait(lock,[&]{ return isNotWaiting;}); } } }; barrier *myBarrier; class Thread { private: std::thread* cppthread; static void threadFunction(Thread* arg) { arg->run(); } public: Thread() {} Thread(const Thread&) = delete; virtual ~Thread() {delete cppthread;} virtual void run() = 0; void start() { cppthread = new std::thread(Thread::threadFunction, this); } void wait() { cppthread->join(); } }; class BarrierDemo: public Thread { int id; public: BarrierDemo(int i) { id = i; } void run() { std::cout << "Thread " << id << "runs before barrier" << std::endl; myBarrier->wait(); std::cout << "Thread " << id << "runs after barrier" << std::endl; } }; int main() { // your code goes here int threads; std::cin >> threads; myBarrier = new barrier(threads); BarrierDemo* bardemos = static_cast<BarrierDemo*>(::operator new(sizeof(BarrierDemo)*threads)); for (int i = 0; i < threads; i++) { new (&bardemos[i])BarrierDemo(i); bardemos[i].start(); } for (int i = 0; i < threads; i++) { bardemos[i].wait(); } ::operator delete(bardemos); delete myBarrier; return 0; }
Source: https://habr.com/ru/post/246947/
All Articles