/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * Copyright 2018 Danny Robson */ #ifndef CRUFT_UTIL_THREAD_MONITOR_HPP #define CRUFT_UTIL_THREAD_MONITOR_HPP #include #include #include namespace util::thread { template class monitor { public: template monitor (Args &&...args): m_value (std::forward (args)...) { ; } class proxy { public: proxy (MutexT &_mutex, ValueT &_value): m_guard (_mutex), m_value (_value) { ; } ValueT* operator-> () { return &m_value; } private: std::lock_guard m_guard; ValueT &m_value; }; auto acquire (void) { return proxy (m_mutex, m_value); } auto operator-> () { return acquire (); } private: MutexT m_mutex; ValueT m_value; }; }; #endif