#pragma once

#include <vector>
#include <mutex>
#include <shared_mutex>
#include <optional>

namespace th_safe {
    template<typename T>
    class vector {
    public:
        void push_back(const T& value) {
            std::unique_lock lock(mutex_);
            data_.push_back(value);
        }

        std::optional<T> back() const {
            std::shared_lock lock(mutex_);
            if (data_.empty()) return std::nullopt;
            return data_.back();
        }

        std::size_t size() const {
            std::shared_lock lock(mutex_);
            return data_.size();
        }

    private:
        mutable std::shared_mutex mutex_;
        std::vector<T> data_;
    };
}