using semaphores instead of mutex

main
Benjamin Kraft 1 year ago
parent c958221382
commit 383561c3f5
  1. 5
      src/GLWidget.cpp
  2. 6
      src/Simulation.cpp
  3. 4
      src/Simulation.h

@ -41,6 +41,7 @@ void GLWidget::initializeGL() {
glClearDepth(1); glClearDepth(1);
glClearColor(.15, .15, .15, 1); glClearColor(.15, .15, .15, 1);
simulation->semaphore.acquire();
uploadStaticDataToGPU(); uploadStaticDataToGPU();
overlay->init(); overlay->init();
@ -180,7 +181,7 @@ void GLWidget::uploadStaticDataToGPU() {
glBindVertexArray(0); glBindVertexArray(0);
simulation->pendulaMutex.unlock(); simulation->semaphore.release();
} }
void GLWidget::changePosition() { void GLWidget::changePosition() {
@ -196,7 +197,7 @@ void GLWidget::changePosition() {
} }
} }
glUnmapBuffer(GL_ARRAY_BUFFER); glUnmapBuffer(GL_ARRAY_BUFFER);
simulation->pendulaMutex.unlock(); simulation->semaphore.release();
} }
void GLWidget::resizeGL(int w, int h) { void GLWidget::resizeGL(int w, int h) {

@ -24,7 +24,7 @@ void Simulation::update() {
double h = (timescale * updateInterval) / 1000 / substeps; double h = (timescale * updateInterval) / 1000 / substeps;
pendulaMutex.lock(); semaphore.acquire();
double newPotentialEnergy = 0; double newPotentialEnergy = 0;
double newKineticEnergy = 0; double newKineticEnergy = 0;
@ -48,7 +48,7 @@ void Simulation::update() {
} }
void Simulation::clearPendula() { void Simulation::clearPendula() {
pendulaMutex.lock(); semaphore.acquire();
for (Pendulum* &p : pendula){ for (Pendulum* &p : pendula){
delete p; delete p;
@ -64,7 +64,7 @@ void Simulation::clearPendula() {
} }
void Simulation::addPendula(const std::vector<Pendulum *> &add) { void Simulation::addPendula(const std::vector<Pendulum *> &add) {
pendulaMutex.lock(); semaphore.acquire();
for (auto p : add) for (auto p : add)
pendula.push_back(p); pendula.push_back(p);

@ -2,8 +2,8 @@
#include <cstdint> #include <cstdint>
#include <QObject> #include <QObject>
#include <chrono> #include <chrono>
#include <mutex>
#include "Pendulum.h" #include "Pendulum.h"
#include <semaphore>
using namespace std::chrono; using namespace std::chrono;
@ -27,7 +27,7 @@ public:
std::vector<Pendulum *> pendula; std::vector<Pendulum *> pendula;
std::mutex pendulaMutex; std::binary_semaphore semaphore = std::binary_semaphore(1);
FPS * ups; FPS * ups;
void updateEnergy(); void updateEnergy();

Loading…
Cancel
Save