Changeset View
Changeset View
Standalone View
Standalone View
src/rwcollection.h
Show First 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | |||||
template <typename T> class RWCollection { | template <typename T> class RWCollection { | ||||
private: | private: | ||||
T collection; | T collection; | ||||
mutable std::shared_mutex rwmutex; | mutable std::shared_mutex rwmutex; | ||||
public: | public: | ||||
RWCollection() : collection() {} | RWCollection() : collection() {} | ||||
explicit RWCollection(T &&collection_) | |||||
: collection(std::move(collection_)) {} | |||||
using ReadView = | using ReadView = | ||||
RWCollectionView<const T, std::shared_lock<std::shared_mutex>>; | RWCollectionView<const T, std::shared_lock<std::shared_mutex>>; | ||||
ReadView getReadView() const { | ReadView getReadView() const { | ||||
return ReadView(std::shared_lock<std::shared_mutex>(rwmutex), | return ReadView(std::shared_lock<std::shared_mutex>(rwmutex), | ||||
collection); | collection); | ||||
} | } | ||||
using WriteView = RWCollectionView<T, std::unique_lock<std::shared_mutex>>; | using WriteView = RWCollectionView<T, std::unique_lock<std::shared_mutex>>; | ||||
WriteView getWriteView() { | WriteView getWriteView() { | ||||
return WriteView(std::unique_lock<std::shared_mutex>(rwmutex), | return WriteView(std::unique_lock<std::shared_mutex>(rwmutex), | ||||
collection); | collection); | ||||
} | } | ||||
}; | }; | ||||
#endif // BITCOIN_RWCOLLECTION_H | #endif // BITCOIN_RWCOLLECTION_H |