Loading GUI/ThreadSafeVector.h 0 → 100644 +45 −0 Original line number Diff line number Diff line // // Created by Ari Trachtenberg on 11/8/24. // #ifndef HW6_THREADSAFEVECTOR_H #define HW6_THREADSAFEVECTOR_H #include <vector> #include <shared_mutex> #include <mutex> #include <stdexcept> // A thread-safe vector, based on a ChatGPT response template <typename T> class ThreadSafeVector { private: std::vector<T> vec; mutable std::shared_mutex mtx; // `mutable` allows locking in const methods. public: // Pushes an element to the back of the vector (exclusive lock for modification). void push_back(const T& value) { std::unique_lock<std::shared_mutex> lock(mtx); vec.push_back(value); } // Returns the size of the vector (shared lock for read access). size_t size() const { std::shared_lock<std::shared_mutex> lock(mtx); return vec.size(); } // Accesses an element by index with bounds checking (shared lock for read access). T operator[](size_t index) const { std::shared_lock<std::shared_mutex> lock(mtx); return vec[index]; } // Provides a thread-safe way to begin and end iterators (copy for safe iteration). std::vector<T> get_copy() const { std::shared_lock<std::shared_mutex> lock(mtx); return vec; // Returns a copy of the vector. } }; #endif //HW6_THREADSAFEVECTOR_H GUI/Window.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -26,7 +26,7 @@ void draw::Window::_drawShapes() { } // draw shapes for (Shape *theShape: shapeList) { for (Shape *theShape: shapeList.get_copy()) { theShape->onInput(userInput); theShape->draw(); } Loading GUI/Window.h +2 −1 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ #include "../Shapes/Shape.h" #include "../Shapes/Rect/Rect.h" #include "../Shapes/AllDoneSensor.h" #include "ThreadSafeVector.h" using namespace std; Loading Loading @@ -146,7 +147,7 @@ namespace draw { /** * A list of the shapes that are displayed on this Window. */ vector<Shape *> shapeList; ThreadSafeVector<Shape *> shapeList; /** * The Thread used for drawing shapes. Loading Loading
GUI/ThreadSafeVector.h 0 → 100644 +45 −0 Original line number Diff line number Diff line // // Created by Ari Trachtenberg on 11/8/24. // #ifndef HW6_THREADSAFEVECTOR_H #define HW6_THREADSAFEVECTOR_H #include <vector> #include <shared_mutex> #include <mutex> #include <stdexcept> // A thread-safe vector, based on a ChatGPT response template <typename T> class ThreadSafeVector { private: std::vector<T> vec; mutable std::shared_mutex mtx; // `mutable` allows locking in const methods. public: // Pushes an element to the back of the vector (exclusive lock for modification). void push_back(const T& value) { std::unique_lock<std::shared_mutex> lock(mtx); vec.push_back(value); } // Returns the size of the vector (shared lock for read access). size_t size() const { std::shared_lock<std::shared_mutex> lock(mtx); return vec.size(); } // Accesses an element by index with bounds checking (shared lock for read access). T operator[](size_t index) const { std::shared_lock<std::shared_mutex> lock(mtx); return vec[index]; } // Provides a thread-safe way to begin and end iterators (copy for safe iteration). std::vector<T> get_copy() const { std::shared_lock<std::shared_mutex> lock(mtx); return vec; // Returns a copy of the vector. } }; #endif //HW6_THREADSAFEVECTOR_H
GUI/Window.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -26,7 +26,7 @@ void draw::Window::_drawShapes() { } // draw shapes for (Shape *theShape: shapeList) { for (Shape *theShape: shapeList.get_copy()) { theShape->onInput(userInput); theShape->draw(); } Loading
GUI/Window.h +2 −1 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ #include "../Shapes/Shape.h" #include "../Shapes/Rect/Rect.h" #include "../Shapes/AllDoneSensor.h" #include "ThreadSafeVector.h" using namespace std; Loading Loading @@ -146,7 +147,7 @@ namespace draw { /** * A list of the shapes that are displayed on this Window. */ vector<Shape *> shapeList; ThreadSafeVector<Shape *> shapeList; /** * The Thread used for drawing shapes. Loading