Danny Robson
f6056153e3
This places, at long last, the core library code into the same namespace as the extended library code.
53 lines
1.2 KiB
C++
53 lines
1.2 KiB
C++
/*
|
|
* 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 <danny@nerdcruft.net>
|
|
*/
|
|
|
|
#ifndef CRUFT_UTIL_THREAD_MONITOR_HPP
|
|
#define CRUFT_UTIL_THREAD_MONITOR_HPP
|
|
|
|
#include <mutex>
|
|
#include <utility>
|
|
#include <functional>
|
|
|
|
|
|
namespace cruft::thread {
|
|
template <typename ValueT, typename MutexT = std::mutex>
|
|
class monitor {
|
|
public:
|
|
template <typename ...Args>
|
|
monitor (Args &&...args):
|
|
m_value (std::forward<Args> (args)...)
|
|
{ ; }
|
|
|
|
class proxy {
|
|
public:
|
|
proxy (MutexT &_mutex, ValueT &_value):
|
|
m_guard (_mutex),
|
|
m_value (_value)
|
|
{ ; }
|
|
|
|
ValueT* operator-> ()
|
|
{
|
|
return &m_value;
|
|
}
|
|
|
|
private:
|
|
std::lock_guard<MutexT> 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
|