From 5ca04e5b2ec8eef88df6cbd4e3d09ac7dab55c0d Mon Sep 17 00:00:00 2001 From: Julian T Date: Fri, 14 Aug 2020 18:53:08 +0200 Subject: Added multithreading --- app/rendercoord.hpp | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'app/rendercoord.hpp') diff --git a/app/rendercoord.hpp b/app/rendercoord.hpp index 113b4dc..a749f8a 100644 --- a/app/rendercoord.hpp +++ b/app/rendercoord.hpp @@ -2,6 +2,7 @@ #define RENDER_THREAD_H #include "draw.hpp" +#include #include #include @@ -16,10 +17,17 @@ class RenderThread : public QThread { Q_OBJECT public: - RenderThread(Renderer r, QObject *parent = nullptr, unsigned id = 0); + RenderThread(Renderer r, unsigned threads, QObject *parent = nullptr, unsigned id = 0); // Returns 0 if successful or 1 if busy int render(QRgb *buffer, unsigned samples); + + void pause(); + void resume(); + + unsigned stop(); + void stopAt(int at); + unsigned current_samples(); signals: @@ -31,18 +39,21 @@ class RenderThread : public QThread { QSemaphore m_lock; QRgb *m_writebuffer; - unsigned m_samples; - unsigned m_current_samples; + std::atomic_int m_samples; + std::atomic_int m_current_samples; Renderer m_render; + unsigned m_workers; + // Value in here means work is to be done QSemaphore m_work; + QSemaphore m_pause; unsigned m_id; }; -const std::string states[] = { "Stopped", "Running" }; -enum State { stopped, running }; +const std::string states[] = { "Stopped", "Running", "Stopping" }; +enum State { stopped, running, stopping }; class RenderCoordinator : public QObject { Q_OBJECT @@ -54,15 +65,19 @@ class RenderCoordinator : public QObject { public slots: void workerDone(unsigned workerid); + void stop(); private slots: void updateUi(); private: + unsigned calcStats(unsigned *max, unsigned *min, double *avg); + DrawWidget &m_target; Renderer m_renderer; - RenderThread m_worker; + std::vector m_workers; + unsigned m_started; QLabel *m_status; QTimer m_timer; -- cgit v1.2.3