From 383561c3f50ade64669cd6d958ee1556c341755a Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Tue, 19 Sep 2023 23:46:49 +0200 Subject: [PATCH] using semaphores instead of mutex --- src/GLWidget.cpp | 5 +++-- src/Simulation.cpp | 6 +++--- src/Simulation.h | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/GLWidget.cpp b/src/GLWidget.cpp index b7a0e54..382cd45 100644 --- a/src/GLWidget.cpp +++ b/src/GLWidget.cpp @@ -41,6 +41,7 @@ void GLWidget::initializeGL() { glClearDepth(1); glClearColor(.15, .15, .15, 1); + simulation->semaphore.acquire(); uploadStaticDataToGPU(); overlay->init(); @@ -180,7 +181,7 @@ void GLWidget::uploadStaticDataToGPU() { glBindVertexArray(0); - simulation->pendulaMutex.unlock(); + simulation->semaphore.release(); } void GLWidget::changePosition() { @@ -196,7 +197,7 @@ void GLWidget::changePosition() { } } glUnmapBuffer(GL_ARRAY_BUFFER); - simulation->pendulaMutex.unlock(); + simulation->semaphore.release(); } void GLWidget::resizeGL(int w, int h) { diff --git a/src/Simulation.cpp b/src/Simulation.cpp index 89c1619..dae7836 100644 --- a/src/Simulation.cpp +++ b/src/Simulation.cpp @@ -24,7 +24,7 @@ void Simulation::update() { double h = (timescale * updateInterval) / 1000 / substeps; - pendulaMutex.lock(); + semaphore.acquire(); double newPotentialEnergy = 0; double newKineticEnergy = 0; @@ -48,7 +48,7 @@ void Simulation::update() { } void Simulation::clearPendula() { - pendulaMutex.lock(); + semaphore.acquire(); for (Pendulum* &p : pendula){ delete p; @@ -64,7 +64,7 @@ void Simulation::clearPendula() { } void Simulation::addPendula(const std::vector &add) { - pendulaMutex.lock(); + semaphore.acquire(); for (auto p : add) pendula.push_back(p); diff --git a/src/Simulation.h b/src/Simulation.h index c3a204f..b2f6ef2 100644 --- a/src/Simulation.h +++ b/src/Simulation.h @@ -2,8 +2,8 @@ #include #include #include -#include #include "Pendulum.h" +#include using namespace std::chrono; @@ -27,7 +27,7 @@ public: std::vector pendula; - std::mutex pendulaMutex; + std::binary_semaphore semaphore = std::binary_semaphore(1); FPS * ups; void updateEnergy();