FeatherMC/src/Lockable.h

26 lines
601 B
C++

#pragma once
#include <mutex>
#include <vector>
#include <list>
namespace Feather
{
template <typename T>
class Lockable {
public:
auto borrow() { return std::pair< T&, std::lock_guard<std::mutex>>{object, mutex}; }
auto borrow() const { return std::pair<const T&, std::lock_guard<std::mutex>>{object, mutex}; }
private:
T object;
mutable std::mutex mutex;
};
template <typename T>
using LockableVector = Lockable<std::vector<T>>;
template <typename T>
using LockableList = Lockable<std::list<T>>;
}