diff --git a/src/util/BufferPool.h b/src/util/BufferPool.h index 36b27517..c441c385 100644 --- a/src/util/BufferPool.h +++ b/src/util/BufferPool.h @@ -9,6 +9,8 @@ #include namespace util { + /// @brief Thread-safe pool of same-sized buffers + /// @tparam T array type template class BufferPool { std::vector> buffers; @@ -19,6 +21,8 @@ namespace util { BufferPool(size_t bufferSize) : bufferSize(bufferSize) { } + /// @brief Retrieve a buffer for use + /// @return pointer that brings buffer back to the pool when destroyed std::shared_ptr get() { std::lock_guard lock(mutex); if (freeBuffers.empty()) { @@ -28,6 +32,7 @@ namespace util { auto* buffer = freeBuffers.front(); freeBuffers.pop(); return std::shared_ptr(buffer, [this](T* ptr) { + std::lock_guard lock(mutex); freeBuffers.push(ptr); }); }